doxygen: add newline after \file
[blender.git] / source / blender / editors / transform / transform_snap.c
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19
20 /** \file
21  * \ingroup edtransform
22  */
23
24 #include <stdlib.h>
25 #include <math.h>
26 #include <float.h>
27 #include <stdio.h>
28
29 #include "PIL_time.h"
30
31 #include "DNA_scene_types.h"
32 #include "DNA_object_types.h"
33 #include "DNA_meshdata_types.h"  /* Temporary, for snapping to other unselected meshes */
34 #include "DNA_node_types.h"
35 #include "DNA_space_types.h"
36 #include "DNA_screen_types.h"
37 #include "DNA_view3d_types.h"
38 #include "DNA_windowmanager_types.h"
39
40 #include "BLI_math.h"
41 #include "BLI_blenlib.h"
42 #include "BLI_utildefines.h"
43
44 #include "GPU_immediate.h"
45 #include "GPU_state.h"
46
47 #include "BKE_layer.h"
48 #include "BKE_object.h"
49 #include "BKE_anim.h"  /* for duplis */
50 #include "BKE_context.h"
51 #include "BKE_editmesh.h"
52 #include "BKE_sequencer.h"
53 #include "BKE_main.h"
54
55 #include "RNA_access.h"
56
57 #include "WM_types.h"
58
59 #include "ED_image.h"
60 #include "ED_node.h"
61 #include "ED_uvedit.h"
62 #include "ED_view3d.h"
63 #include "ED_transform_snap_object_context.h"
64
65 #include "DEG_depsgraph.h"
66
67 #include "UI_resources.h"
68 #include "UI_view2d.h"
69
70 #include "MEM_guardedalloc.h"
71
72 #include "transform.h"
73
74 /* this should be passed as an arg for use in snap functions */
75 #undef BASACT
76
77 /* use half of flt-max so we can scale up without an exception */
78
79 /********************* PROTOTYPES ***********************/
80
81 static void setSnappingCallback(TransInfo *t);
82
83 static void ApplySnapTranslation(TransInfo *t, float vec[3]);
84 static void ApplySnapRotation(TransInfo *t, float *vec);
85 static void ApplySnapResize(TransInfo *t, float vec[2]);
86
87 /* static void CalcSnapGrid(TransInfo *t, float *vec); */
88 static void CalcSnapGeometry(TransInfo *t, float *vec);
89
90 static void TargetSnapMedian(TransInfo *t);
91 static void TargetSnapCenter(TransInfo *t);
92 static void TargetSnapClosest(TransInfo *t);
93 static void TargetSnapActive(TransInfo *t);
94
95 static float RotationBetween(TransInfo *t, const float p1[3], const float p2[3]);
96 static float TranslationBetween(TransInfo *t, const float p1[3], const float p2[3]);
97 static float ResizeBetween(TransInfo *t, const float p1[3], const float p2[3]);
98
99
100 /****************** IMPLEMENTATIONS *********************/
101
102 static bool snapNodeTest(View2D *v2d, bNode *node, eSnapSelect snap_select);
103 static NodeBorder snapNodeBorder(int snap_node_mode);
104
105 #if 0
106 int BIF_snappingSupported(Object *obedit)
107 {
108         int status = 0;
109
110         /* only support object mesh, armature, curves */
111         if (obedit == NULL || ELEM(obedit->type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE, OB_MBALL)) {
112                 status = 1;
113         }
114
115         return status;
116 }
117 #endif
118
119 bool validSnap(const TransInfo *t)
120 {
121         return (t->tsnap.status & (POINT_INIT | TARGET_INIT)) == (POINT_INIT | TARGET_INIT) ||
122                (t->tsnap.status & (MULTI_POINTS | TARGET_INIT)) == (MULTI_POINTS | TARGET_INIT);
123 }
124
125 bool activeSnap(const TransInfo *t)
126 {
127         return ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP) ||
128                ((t->modifiers & (MOD_SNAP | MOD_SNAP_INVERT)) == MOD_SNAP_INVERT);
129 }
130
131 bool transformModeUseSnap(const TransInfo *t)
132 {
133         ToolSettings *ts = t->settings;
134         if (t->mode == TFM_TRANSLATION) {
135                 return (ts->snap_transform_mode_flag & SCE_SNAP_TRANSFORM_MODE_TRANSLATE) != 0;
136         }
137         if (t->mode == TFM_ROTATION) {
138                 return (ts->snap_transform_mode_flag & SCE_SNAP_TRANSFORM_MODE_ROTATE) != 0;
139         }
140         if (t->mode == TFM_RESIZE) {
141                 return (ts->snap_transform_mode_flag & SCE_SNAP_TRANSFORM_MODE_SCALE) != 0;
142         }
143
144         return false;
145 }
146
147 static bool doForceIncrementSnap(const TransInfo *t)
148 {
149         return !transformModeUseSnap(t);
150 }
151
152 void drawSnapping(const struct bContext *C, TransInfo *t)
153 {
154         unsigned char col[4], selectedCol[4], activeCol[4];
155
156         if (!activeSnap(t))
157                 return;
158
159         UI_GetThemeColor3ubv(TH_TRANSFORM, col);
160         col[3] = 128;
161
162         UI_GetThemeColor3ubv(TH_SELECT, selectedCol);
163         selectedCol[3] = 128;
164
165         UI_GetThemeColor3ubv(TH_ACTIVE, activeCol);
166         activeCol[3] = 192;
167
168         if (t->spacetype == SPACE_VIEW3D) {
169                 if (validSnap(t)) {
170                         TransSnapPoint *p;
171                         RegionView3D *rv3d = CTX_wm_region_view3d(C);
172                         float imat[4][4];
173                         float size;
174
175                         GPU_depth_test(false);
176
177                         size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
178
179                         invert_m4_m4(imat, rv3d->viewmat);
180
181                         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 3, GPU_FETCH_FLOAT);
182
183                         immBindBuiltinProgram(GPU_SHADER_3D_UNIFORM_COLOR);
184
185                         for (p = t->tsnap.points.first; p; p = p->next) {
186                                 if (p == t->tsnap.selectedPoint) {
187                                         immUniformColor4ubv(selectedCol);
188                                 }
189                                 else {
190                                         immUniformColor4ubv(col);
191                                 }
192
193                                 imm_drawcircball(p->co, ED_view3d_pixel_size(rv3d, p->co) * size * 0.75f, imat, pos);
194                         }
195
196                         if (t->tsnap.status & POINT_INIT) {
197                                 immUniformColor4ubv(activeCol);
198
199                                 imm_drawcircball(t->tsnap.snapPoint, ED_view3d_pixel_size(rv3d, t->tsnap.snapPoint) * size, imat, pos);
200                         }
201
202                         /* draw normal if needed */
203                         if (usingSnappingNormal(t) && validSnappingNormal(t)) {
204                                 immUniformColor4ubv(activeCol);
205
206                                 immBegin(GPU_PRIM_LINES, 2);
207                                 immVertex3f(pos, t->tsnap.snapPoint[0], t->tsnap.snapPoint[1], t->tsnap.snapPoint[2]);
208                                 immVertex3f(pos, t->tsnap.snapPoint[0] + t->tsnap.snapNormal[0],
209                                             t->tsnap.snapPoint[1] + t->tsnap.snapNormal[1],
210                                             t->tsnap.snapPoint[2] + t->tsnap.snapNormal[2]);
211                                 immEnd();
212                         }
213
214                         immUnbindProgram();
215
216                         GPU_depth_test(true);
217                 }
218         }
219         else if (t->spacetype == SPACE_IMAGE) {
220                 if (validSnap(t)) {
221                         /* This will not draw, and Im nor sure why - campbell */
222                         /* TODO: see 2.7x for non-working code */
223                 }
224         }
225         else if (t->spacetype == SPACE_NODE) {
226                 if (validSnap(t)) {
227                         ARegion *ar = CTX_wm_region(C);
228                         TransSnapPoint *p;
229                         float size;
230
231                         size = 2.5f * UI_GetThemeValuef(TH_VERTEX_SIZE);
232
233                         GPU_blend(true);
234
235                         uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
236
237                         immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
238
239                         for (p = t->tsnap.points.first; p; p = p->next) {
240                                 if (p == t->tsnap.selectedPoint) {
241                                         immUniformColor4ubv(selectedCol);
242                                 }
243                                 else {
244                                         immUniformColor4ubv(col);
245                                 }
246
247                                 ED_node_draw_snap(&ar->v2d, p->co, size, 0, pos);
248                         }
249
250                         if (t->tsnap.status & POINT_INIT) {
251                                 immUniformColor4ubv(activeCol);
252
253                                 ED_node_draw_snap(&ar->v2d, t->tsnap.snapPoint, size, t->tsnap.snapNodeBorder, pos);
254                         }
255
256                         immUnbindProgram();
257
258                         GPU_blend(false);
259                 }
260         }
261 }
262
263 eRedrawFlag handleSnapping(TransInfo *t, const wmEvent *event)
264 {
265         eRedrawFlag status = TREDRAW_NOTHING;
266
267 #if 0 // XXX need a proper selector for all snap mode
268         if (BIF_snappingSupported(t->obedit) && event->type == TABKEY && event->shift) {
269                 /* toggle snap and reinit */
270                 t->settings->snap_flag ^= SCE_SNAP;
271                 initSnapping(t, NULL);
272                 status = TREDRAW_HARD;
273         }
274 #endif
275         if (event->type == MOUSEMOVE) {
276                 status |= updateSelectedSnapPoint(t);
277         }
278
279         return status;
280 }
281
282 void applyProject(TransInfo *t)
283 {
284         /* XXX FLICKER IN OBJECT MODE */
285         if ((t->tsnap.project) && activeSnap(t) && (t->flag & T_NO_PROJECT) == 0) {
286                 float tvec[3];
287                 int i;
288
289                 FOREACH_TRANS_DATA_CONTAINER(t, tc) {
290                         TransData *td = tc->data;
291                         for (i = 0; i < tc->data_len; i++, td++) {
292                                 float iloc[3], loc[3], no[3];
293                                 float mval_fl[2];
294
295                                 if (td->flag & TD_NOACTION)
296                                         break;
297
298                                 if (td->flag & TD_SKIP)
299                                         continue;
300
301                                 if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
302                                         continue;
303
304                                 copy_v3_v3(iloc, td->loc);
305                                 if (tc->use_local_mat) {
306                                         mul_m4_v3(tc->mat, iloc);
307                                 }
308                                 else if (t->flag & T_OBJECT) {
309                                         BKE_object_eval_transform_all(t->depsgraph, t->scene, td->ob);
310                                         copy_v3_v3(iloc, td->ob->obmat[3]);
311                                 }
312
313                                 if (ED_view3d_project_float_global(t->ar, iloc, mval_fl, V3D_PROJ_TEST_NOP) == V3D_PROJ_RET_OK) {
314                                         if (ED_transform_snap_object_project_view3d(
315                                                 t->tsnap.object_context,
316                                                 SCE_SNAP_MODE_FACE,
317                                                 &(const struct SnapObjectParams){
318                                                     .snap_select = t->tsnap.modeSelect,
319                                                     .use_object_edit_cage = (t->flag & T_EDIT) != 0,
320                                                     .use_occlusion_test = false,
321                                                 },
322                                                 mval_fl, 0, loc, no))
323                                         {
324 #if 0
325                                                 if (tc->use_local_mat) {
326                                                         mul_m4_v3(tc->imat, loc);
327                                                 }
328 #endif
329
330                                                 sub_v3_v3v3(tvec, loc, iloc);
331
332                                                 mul_m3_v3(td->smtx, tvec);
333
334                                                 add_v3_v3(td->loc, tvec);
335
336                                                 if (t->tsnap.align && (t->flag & T_OBJECT)) {
337                                                         /* handle alignment as well */
338                                                         const float *original_normal;
339                                                         float mat[3][3];
340
341                                                         /* In pose mode, we want to align normals with Y axis of bones... */
342                                                         original_normal = td->axismtx[2];
343
344                                                         rotation_between_vecs_to_mat3(mat, original_normal, no);
345
346                                                         transform_data_ext_rotate(td, mat, true);
347
348                                                         /* TODO support constraints for rotation too? see ElementRotation */
349                                                 }
350                                         }
351                                 }
352
353                                 //XXX constraintTransLim(t, td);
354                         }
355                 }
356         }
357 }
358
359 void applyGridAbsolute(TransInfo *t)
360 {
361         float grid_size = 0.0f;
362         GearsType grid_action;
363         int i;
364
365         if (!(activeSnap(t) && (t->tsnap.mode & (SCE_SNAP_MODE_INCREMENT | SCE_SNAP_MODE_GRID))))
366                 return;
367
368         grid_action = BIG_GEARS;
369         if (t->modifiers & MOD_PRECISION)
370                 grid_action = SMALL_GEARS;
371
372         switch (grid_action) {
373                 case NO_GEARS: grid_size = t->snap_spatial[0]; break;
374                 case BIG_GEARS: grid_size = t->snap_spatial[1]; break;
375                 case SMALL_GEARS: grid_size = t->snap_spatial[2]; break;
376         }
377         /* early exit on unusable grid size */
378         if (grid_size == 0.0f)
379                 return;
380
381         FOREACH_TRANS_DATA_CONTAINER(t, tc) {
382                 TransData *td;
383
384                 for (i = 0, td = tc->data; i < tc->data_len; i++, td++) {
385                         float iloc[3], loc[3], tvec[3];
386
387                         if (td->flag & TD_NOACTION)
388                                 break;
389
390                         if (td->flag & TD_SKIP)
391                                 continue;
392
393                         if ((t->flag & T_PROP_EDIT) && (td->factor == 0.0f))
394                                 continue;
395
396                         copy_v3_v3(iloc, td->loc);
397                         if (tc->use_local_mat) {
398                                 mul_m4_v3(tc->mat, iloc);
399                         }
400                         else if (t->flag & T_OBJECT) {
401                                 BKE_object_eval_transform_all(t->depsgraph, t->scene, td->ob);
402                                 copy_v3_v3(iloc, td->ob->obmat[3]);
403                         }
404
405                         mul_v3_v3fl(loc, iloc, 1.0f / grid_size);
406                         loc[0] = roundf(loc[0]);
407                         loc[1] = roundf(loc[1]);
408                         loc[2] = roundf(loc[2]);
409                         mul_v3_fl(loc, grid_size);
410
411                         sub_v3_v3v3(tvec, loc, iloc);
412                         mul_m3_v3(td->smtx, tvec);
413                         add_v3_v3(td->loc, tvec);
414                 }
415         }
416 }
417
418 void applySnapping(TransInfo *t, float *vec)
419 {
420         /* Each Trans Data already makes the snap to face */
421         if (doForceIncrementSnap(t) ||
422             (t->tsnap.project && t->tsnap.mode == SCE_SNAP_MODE_FACE))
423         {
424                 return;
425         }
426
427         if (t->tsnap.status & SNAP_FORCED) {
428                 t->tsnap.targetSnap(t);
429
430                 t->tsnap.applySnap(t, vec);
431         }
432         else if (((t->tsnap.mode & ~(SCE_SNAP_MODE_INCREMENT | SCE_SNAP_MODE_GRID)) != 0) &&
433                  activeSnap(t))
434         {
435                 double current = PIL_check_seconds_timer();
436
437                 // Time base quirky code to go around findnearest slowness
438                 /* !TODO! add exception for object mode, no need to slow it down then */
439                 if (current - t->tsnap.last >= 0.01) {
440                         t->tsnap.calcSnap(t, vec);
441                         t->tsnap.targetSnap(t);
442
443                         t->tsnap.last = current;
444                 }
445                 if (validSnap(t)) {
446                         t->tsnap.applySnap(t, vec);
447                 }
448         }
449 }
450
451 void resetSnapping(TransInfo *t)
452 {
453         t->tsnap.status = 0;
454         t->tsnap.align = false;
455         t->tsnap.project = 0;
456         t->tsnap.mode = 0;
457         t->tsnap.modeSelect = 0;
458         t->tsnap.target = 0;
459         t->tsnap.last = 0;
460         t->tsnap.applySnap = NULL;
461
462         t->tsnap.snapNormal[0] = 0;
463         t->tsnap.snapNormal[1] = 0;
464         t->tsnap.snapNormal[2] = 0;
465
466         t->tsnap.snapNodeBorder = 0;
467 }
468
469 bool usingSnappingNormal(const TransInfo *t)
470 {
471         return t->tsnap.align;
472 }
473
474 bool validSnappingNormal(const TransInfo *t)
475 {
476         if (validSnap(t)) {
477                 if (!is_zero_v3(t->tsnap.snapNormal)) {
478                         return true;
479                 }
480         }
481
482         return false;
483 }
484
485 static bool bm_edge_is_snap_target(BMEdge *e, void *UNUSED(user_data))
486 {
487         if (BM_elem_flag_test(e, BM_ELEM_SELECT | BM_ELEM_HIDDEN) ||
488             BM_elem_flag_test(e->v1, BM_ELEM_SELECT) ||
489             BM_elem_flag_test(e->v2, BM_ELEM_SELECT))
490         {
491                 return false;
492         }
493
494         return true;
495 }
496
497 static bool bm_face_is_snap_target(BMFace *f, void *UNUSED(user_data))
498 {
499         if (BM_elem_flag_test(f, BM_ELEM_SELECT | BM_ELEM_HIDDEN)) {
500                 return false;
501         }
502
503         BMLoop *l_iter, *l_first;
504         l_iter = l_first = BM_FACE_FIRST_LOOP(f);
505         do {
506                 if (BM_elem_flag_test(l_iter->v, BM_ELEM_SELECT)) {
507                         return false;
508                 }
509         } while ((l_iter = l_iter->next) != l_first);
510
511         return true;
512 }
513
514 static void initSnappingMode(TransInfo *t)
515 {
516         Main *bmain = CTX_data_main(t->context);
517         ToolSettings *ts = t->settings;
518         /* All obedit types will match. */
519         const int obedit_type = t->data_container->obedit ? t->data_container->obedit->type : -1;
520         ViewLayer *view_layer = t->view_layer;
521         Base *base_act = view_layer->basact;
522
523         if (t->spacetype == SPACE_NODE) {
524                 /* force project off when not supported */
525                 t->tsnap.project = 0;
526
527                 t->tsnap.mode = ts->snap_node_mode;
528         }
529         else if (t->spacetype == SPACE_IMAGE) {
530                 /* force project off when not supported */
531                 t->tsnap.project = 0;
532
533                 t->tsnap.mode = ts->snap_uv_mode;
534         }
535         else {
536                 /* force project off when not supported */
537                 if ((ts->snap_mode & SCE_SNAP_MODE_FACE) == 0)
538                         t->tsnap.project = 0;
539
540                 t->tsnap.mode = ts->snap_mode;
541         }
542
543         if ((t->spacetype == SPACE_VIEW3D || t->spacetype == SPACE_IMAGE) &&  /* Only 3D view or UV */
544             (t->flag & T_CAMERA) == 0)  /* Not with camera selected in camera view */
545         {
546                 setSnappingCallback(t);
547
548                 /* Edit mode */
549                 if (t->tsnap.applySnap != NULL && // A snapping function actually exist
550                     ((obedit_type != -1) && ELEM(obedit_type, OB_MESH, OB_ARMATURE, OB_CURVE, OB_LATTICE, OB_MBALL)) ) // Temporary limited to edit mode meshes, armature, curves, mballs
551                 {
552                         /* Exclude editmesh if using proportional edit */
553                         if ((obedit_type == OB_MESH) && (t->flag & T_PROP_EDIT)) {
554                                 t->tsnap.modeSelect = SNAP_NOT_ACTIVE;
555                         }
556                         else {
557                                 t->tsnap.modeSelect = t->tsnap.snap_self ? SNAP_ALL : SNAP_NOT_ACTIVE;
558                         }
559                 }
560                 /* Particles edit mode*/
561                 else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
562                          ((obedit_type == -1) && base_act && base_act->object && base_act->object->mode & OB_MODE_PARTICLE_EDIT))
563                 {
564                         t->tsnap.modeSelect = SNAP_ALL;
565                 }
566                 /* Object mode */
567                 else if (t->tsnap.applySnap != NULL && // A snapping function actually exist
568                          (obedit_type == -1) ) // Object Mode
569                 {
570                         /* In "Edit Strokes" mode, Snap tool can perform snap to selected or active objects (see T49632)
571                          * TODO: perform self snap in gpencil_strokes */
572                         t->tsnap.modeSelect = (
573                                 ((t->options & (CTX_GPENCIL_STROKES | CTX_CURSOR)) != 0) ?
574                                 SNAP_ALL : SNAP_NOT_SELECTED);
575                 }
576                 else {
577                         /* Grid if snap is not possible */
578                         t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
579                 }
580         }
581         else if (t->spacetype == SPACE_NODE) {
582                 setSnappingCallback(t);
583
584                 if (t->tsnap.applySnap != NULL) {
585                         t->tsnap.modeSelect = SNAP_NOT_SELECTED;
586                 }
587                 else {
588                         /* Grid if snap is not possible */
589                         t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
590                 }
591         }
592         else if (t->spacetype == SPACE_SEQ) {
593                 /* We do our own snapping currently, so nothing here */
594                 t->tsnap.mode = SCE_SNAP_MODE_GRID;  /* Dummy, should we rather add a NOP mode? */
595         }
596         else {
597                 /* Always grid outside of 3D view */
598                 t->tsnap.mode = SCE_SNAP_MODE_INCREMENT;
599         }
600
601         if (t->spacetype == SPACE_VIEW3D) {
602                 if (t->tsnap.object_context == NULL) {
603                         t->tsnap.object_context = ED_transform_snap_object_context_create_view3d(
604                                 bmain, t->scene, t->depsgraph, 0, t->ar, t->view);
605
606                         ED_transform_snap_object_context_set_editmesh_callbacks(
607                                 t->tsnap.object_context,
608                                 (bool (*)(BMVert *, void *))BM_elem_cb_check_hflag_disabled,
609                                 bm_edge_is_snap_target,
610                                 bm_face_is_snap_target,
611                                 POINTER_FROM_UINT((BM_ELEM_SELECT | BM_ELEM_HIDDEN)));
612                 }
613         }
614 }
615
616 void initSnapping(TransInfo *t, wmOperator *op)
617 {
618         ToolSettings *ts = t->settings;
619         short snap_target = t->settings->snap_target;
620
621         resetSnapping(t);
622
623         /* if snap property exists */
624         if (op && RNA_struct_find_property(op->ptr, "snap") && RNA_struct_property_is_set(op->ptr, "snap")) {
625                 if (RNA_boolean_get(op->ptr, "snap")) {
626                         t->modifiers |= MOD_SNAP;
627
628                         if (RNA_struct_property_is_set(op->ptr, "snap_target")) {
629                                 snap_target = RNA_enum_get(op->ptr, "snap_target");
630                         }
631
632                         if (RNA_struct_property_is_set(op->ptr, "snap_point")) {
633                                 RNA_float_get_array(op->ptr, "snap_point", t->tsnap.snapPoint);
634                                 t->tsnap.status |= SNAP_FORCED | POINT_INIT;
635                         }
636
637                         /* snap align only defined in specific cases */
638                         if (RNA_struct_find_property(op->ptr, "snap_align")) {
639                                 t->tsnap.align = RNA_boolean_get(op->ptr, "snap_align");
640                                 RNA_float_get_array(op->ptr, "snap_normal", t->tsnap.snapNormal);
641                                 normalize_v3(t->tsnap.snapNormal);
642                         }
643
644                         if (RNA_struct_find_property(op->ptr, "use_snap_project")) {
645                                 t->tsnap.project = RNA_boolean_get(op->ptr, "use_snap_project");
646                         }
647
648                         if (RNA_struct_find_property(op->ptr, "use_snap_self")) {
649                                 t->tsnap.snap_self = RNA_boolean_get(op->ptr, "use_snap_self");
650                         }
651                 }
652         }
653         /* use scene defaults only when transform is modal */
654         else if (t->flag & T_MODAL) {
655                 if (ELEM(t->spacetype, SPACE_VIEW3D, SPACE_IMAGE, SPACE_NODE)) {
656                         if (transformModeUseSnap(t) && (ts->snap_flag & SCE_SNAP)) {
657                                 t->modifiers |= MOD_SNAP;
658                         }
659
660                         t->tsnap.align = ((t->settings->snap_flag & SCE_SNAP_ROTATE) != 0);
661                         t->tsnap.project = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
662                         t->tsnap.snap_self = !((t->settings->snap_flag & SCE_SNAP_NO_SELF) != 0);
663                         t->tsnap.peel = ((t->settings->snap_flag & SCE_SNAP_PROJECT) != 0);
664                 }
665
666                 /* for now only 3d view (others can be added if we want) */
667                 if (t->spacetype == SPACE_VIEW3D) {
668                         t->tsnap.snap_spatial_grid = ((t->settings->snap_flag & SCE_SNAP_ABS_GRID) != 0);
669                 }
670         }
671
672         t->tsnap.target = snap_target;
673
674         initSnappingMode(t);
675 }
676
677 void freeSnapping(TransInfo *t)
678 {
679         if (t->tsnap.object_context) {
680                 ED_transform_snap_object_context_destroy(t->tsnap.object_context);
681                 t->tsnap.object_context = NULL;
682         }
683 }
684
685 static void setSnappingCallback(TransInfo *t)
686 {
687         t->tsnap.calcSnap = CalcSnapGeometry;
688
689         switch (t->tsnap.target) {
690                 case SCE_SNAP_TARGET_CLOSEST:
691                         t->tsnap.targetSnap = TargetSnapClosest;
692                         break;
693                 case SCE_SNAP_TARGET_CENTER:
694                         t->tsnap.targetSnap = TargetSnapCenter;
695                         break;
696                 case SCE_SNAP_TARGET_MEDIAN:
697                         t->tsnap.targetSnap = TargetSnapMedian;
698                         break;
699                 case SCE_SNAP_TARGET_ACTIVE:
700                         t->tsnap.targetSnap = TargetSnapActive;
701                         break;
702
703         }
704
705         switch (t->mode) {
706                 case TFM_TRANSLATION:
707                         t->tsnap.applySnap = ApplySnapTranslation;
708                         t->tsnap.distance = TranslationBetween;
709                         break;
710                 case TFM_ROTATION:
711                         t->tsnap.applySnap = ApplySnapRotation;
712                         t->tsnap.distance = RotationBetween;
713
714                         // Can't do TARGET_CENTER with rotation, use TARGET_MEDIAN instead
715                         if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
716                                 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
717                                 t->tsnap.targetSnap = TargetSnapMedian;
718                         }
719                         break;
720                 case TFM_RESIZE:
721                         t->tsnap.applySnap = ApplySnapResize;
722                         t->tsnap.distance = ResizeBetween;
723
724                         // Can't do TARGET_CENTER with resize, use TARGET_MEDIAN instead
725                         if (t->tsnap.target == SCE_SNAP_TARGET_CENTER) {
726                                 t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
727                                 t->tsnap.targetSnap = TargetSnapMedian;
728                         }
729                         break;
730                 default:
731                         t->tsnap.applySnap = NULL;
732                         break;
733         }
734 }
735
736 void addSnapPoint(TransInfo *t)
737 {
738         /* Currently only 3D viewport works for snapping points. */
739         if (t->tsnap.status & POINT_INIT && t->spacetype == SPACE_VIEW3D) {
740                 TransSnapPoint *p = MEM_callocN(sizeof(TransSnapPoint), "SnapPoint");
741
742                 t->tsnap.selectedPoint = p;
743
744                 copy_v3_v3(p->co, t->tsnap.snapPoint);
745
746                 BLI_addtail(&t->tsnap.points, p);
747
748                 t->tsnap.status |= MULTI_POINTS;
749         }
750 }
751
752 eRedrawFlag updateSelectedSnapPoint(TransInfo *t)
753 {
754         eRedrawFlag status = TREDRAW_NOTHING;
755
756         if (t->tsnap.status & MULTI_POINTS) {
757                 TransSnapPoint *p, *closest_p = NULL;
758                 float dist_min_sq = TRANSFORM_SNAP_MAX_PX;
759                 const float mval_fl[2] = {t->mval[0], t->mval[1]};
760                 float screen_loc[2];
761
762                 for (p = t->tsnap.points.first; p; p = p->next) {
763                         float dist_sq;
764
765                         if (ED_view3d_project_float_global(t->ar, p->co, screen_loc, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
766                                 continue;
767                         }
768
769                         dist_sq = len_squared_v2v2(mval_fl, screen_loc);
770
771                         if (dist_sq < dist_min_sq) {
772                                 closest_p = p;
773                                 dist_min_sq = dist_sq;
774                         }
775                 }
776
777                 if (closest_p) {
778                         if (t->tsnap.selectedPoint != closest_p) {
779                                 status = TREDRAW_HARD;
780                         }
781
782                         t->tsnap.selectedPoint = closest_p;
783                 }
784         }
785
786         return status;
787 }
788
789 void removeSnapPoint(TransInfo *t)
790 {
791         if (t->tsnap.status & MULTI_POINTS) {
792                 updateSelectedSnapPoint(t);
793
794                 if (t->tsnap.selectedPoint) {
795                         BLI_freelinkN(&t->tsnap.points, t->tsnap.selectedPoint);
796
797                         if (BLI_listbase_is_empty(&t->tsnap.points)) {
798                                 t->tsnap.status &= ~MULTI_POINTS;
799                         }
800
801                         t->tsnap.selectedPoint = NULL;
802                 }
803
804         }
805 }
806
807 void getSnapPoint(const TransInfo *t, float vec[3])
808 {
809         if (t->tsnap.points.first) {
810                 TransSnapPoint *p;
811                 int total = 0;
812
813                 vec[0] = vec[1] = vec[2] = 0;
814
815                 for (p = t->tsnap.points.first; p; p = p->next, total++) {
816                         add_v3_v3(vec, p->co);
817                 }
818
819                 if (t->tsnap.status & POINT_INIT) {
820                         add_v3_v3(vec, t->tsnap.snapPoint);
821                         total++;
822                 }
823
824                 mul_v3_fl(vec, 1.0f / total);
825         }
826         else {
827                 copy_v3_v3(vec, t->tsnap.snapPoint);
828         }
829 }
830
831 /********************** APPLY **************************/
832
833 static void ApplySnapTranslation(TransInfo *t, float vec[3])
834 {
835         float point[3];
836         getSnapPoint(t, point);
837
838         if (t->spacetype == SPACE_NODE) {
839                 char border = t->tsnap.snapNodeBorder;
840                 if (border & (NODE_LEFT | NODE_RIGHT))
841                         vec[0] = point[0] - t->tsnap.snapTarget[0];
842                 if (border & (NODE_BOTTOM | NODE_TOP))
843                         vec[1] = point[1] - t->tsnap.snapTarget[1];
844         }
845         else {
846                 if (t->spacetype == SPACE_VIEW3D) {
847                         if (t->options & CTX_PAINT_CURVE) {
848                                 if (ED_view3d_project_float_global(t->ar, point, point, V3D_PROJ_TEST_NOP) != V3D_PROJ_RET_OK) {
849                                         zero_v3(point);  /* no good answer here... */
850                                 }
851                         }
852                 }
853
854                 sub_v3_v3v3(vec, point, t->tsnap.snapTarget);
855         }
856 }
857
858 static void ApplySnapRotation(TransInfo *t, float *value)
859 {
860         float point[3];
861         getSnapPoint(t, point);
862
863         float dist = RotationBetween(t, t->tsnap.snapTarget, point);
864         *value = dist;
865 }
866
867 static void ApplySnapResize(TransInfo *t, float vec[3])
868 {
869         float point[3];
870         getSnapPoint(t, point);
871
872         float dist = ResizeBetween(t, t->tsnap.snapTarget, point);
873         copy_v3_fl(vec, dist);
874 }
875
876 /********************** DISTANCE **************************/
877
878 static float TranslationBetween(TransInfo *UNUSED(t), const float p1[3], const float p2[3])
879 {
880         return len_squared_v3v3(p1, p2);
881 }
882
883 static float RotationBetween(
884         TransInfo *t, const float p1[3], const float p2[3])
885 {
886         float angle, start[3], end[3];
887
888         sub_v3_v3v3(start, p1, t->center_global);
889         sub_v3_v3v3(end,   p2, t->center_global);
890
891         // Angle around a constraint axis (error prone, will need debug)
892         if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
893                 float axis[3], tmp[3];
894
895                 t->con.applyRot(t, NULL, NULL, axis, NULL);
896
897                 project_v3_v3v3(tmp, end, axis);
898                 sub_v3_v3v3(end, end, tmp);
899
900                 project_v3_v3v3(tmp, start, axis);
901                 sub_v3_v3v3(start, start, tmp);
902
903                 normalize_v3(end);
904                 normalize_v3(start);
905
906                 cross_v3_v3v3(tmp, start, end);
907
908                 if (dot_v3v3(tmp, axis) < 0.0f)
909                         angle = -acosf(dot_v3v3(start, end));
910                 else
911                         angle = acosf(dot_v3v3(start, end));
912         }
913         else {
914                 float mtx[3][3];
915
916                 copy_m3_m4(mtx, t->viewmat);
917
918                 mul_m3_v3(mtx, end);
919                 mul_m3_v3(mtx, start);
920
921                 angle = atan2f(start[1], start[0]) - atan2f(end[1], end[0]);
922         }
923
924         if (angle > (float)M_PI) {
925                 angle = angle - 2 * (float)M_PI;
926         }
927         else if (angle < -((float)M_PI)) {
928                 angle = 2.0f * (float)M_PI + angle;
929         }
930
931         return angle;
932 }
933
934 static float ResizeBetween(TransInfo *t, const float p1[3], const float p2[3])
935 {
936         float d1[3], d2[3], len_d1;
937
938         sub_v3_v3v3(d1, p1, t->center_global);
939         sub_v3_v3v3(d2, p2, t->center_global);
940
941         if (t->con.applyRot != NULL && (t->con.mode & CON_APPLY)) {
942                 mul_m3_v3(t->con.pmtx, d1);
943                 mul_m3_v3(t->con.pmtx, d2);
944         }
945
946         project_v3_v3v3(d1, d1, d2);
947
948         len_d1 = len_v3(d1);
949
950         /* Use 'invalid' dist when `center == p1` (after projecting),
951          * in this case scale will _never_ move the point in relation to the center,
952          * so it makes no sense to take it into account when scaling. see: T46503 */
953         return len_d1 != 0.0f ? len_v3(d2) / len_d1 : TRANSFORM_DIST_INVALID;
954 }
955
956 /********************** CALC **************************/
957
958 static void UNUSED_FUNCTION(CalcSnapGrid) (TransInfo *t, float *UNUSED(vec))
959 {
960         snapGridIncrementAction(t, t->tsnap.snapPoint, BIG_GEARS);
961 }
962
963 static void CalcSnapGeometry(TransInfo *t, float *UNUSED(vec))
964 {
965         if (t->spacetype == SPACE_VIEW3D) {
966                 float loc[3];
967                 float no[3];
968                 float mval[2];
969                 bool found = false;
970                 float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
971
972                 mval[0] = t->mval[0];
973                 mval[1] = t->mval[1];
974
975                 if (t->tsnap.mode &
976                     (SCE_SNAP_MODE_VERTEX |
977                      SCE_SNAP_MODE_EDGE   |
978                      SCE_SNAP_MODE_FACE))
979                 {
980                         zero_v3(no);  /* objects won't set this */
981                         found = snapObjectsTransform(
982                                 t, mval, &dist_px,
983                                 loc, no);
984                 }
985                 if ((found == false) && (t->tsnap.mode & SCE_SNAP_MODE_VOLUME)) {
986                         found = peelObjectsTransform(
987                                 t, mval,
988                                 (t->settings->snap_flag & SCE_SNAP_PEEL_OBJECT) != 0,
989                                 loc, no, NULL);
990                 }
991
992                 if (found == true) {
993                         copy_v3_v3(t->tsnap.snapPoint, loc);
994                         copy_v3_v3(t->tsnap.snapNormal, no);
995
996                         t->tsnap.status |=  POINT_INIT;
997                 }
998                 else {
999                         t->tsnap.status &= ~POINT_INIT;
1000                 }
1001         }
1002         else if (t->spacetype == SPACE_IMAGE && t->obedit_type == OB_MESH) {
1003                 if (t->tsnap.mode & SCE_SNAP_MODE_VERTEX) {
1004                         Image *ima = ED_space_image(t->sa->spacedata.first);
1005                         float co[2];
1006
1007                         UI_view2d_region_to_view(&t->ar->v2d, t->mval[0], t->mval[1], &co[0], &co[1]);
1008
1009                         uint objects_len = 0;
1010                         Object **objects = BKE_view_layer_array_from_objects_in_edit_mode_unique_data_with_uvs(
1011                                 t->view_layer, NULL, &objects_len);
1012
1013                         float dist_sq = FLT_MAX;
1014                         if (ED_uvedit_nearest_uv_multi(t->scene, ima, objects, objects_len, co, &dist_sq, t->tsnap.snapPoint)) {
1015                                 t->tsnap.snapPoint[0] *= t->aspect[0];
1016                                 t->tsnap.snapPoint[1] *= t->aspect[1];
1017
1018                                 t->tsnap.status |= POINT_INIT;
1019                         }
1020                         else {
1021                                 t->tsnap.status &= ~POINT_INIT;
1022                         }
1023                         MEM_freeN(objects);
1024                 }
1025         }
1026         else if (t->spacetype == SPACE_NODE) {
1027                 if (t->tsnap.mode & (SCE_SNAP_MODE_NODE_X | SCE_SNAP_MODE_NODE_Y)) {
1028                         float loc[2];
1029                         float dist_px = SNAP_MIN_DISTANCE; // Use a user defined value here
1030                         char node_border;
1031
1032                         if (snapNodesTransform(t, t->mval, loc, &dist_px, &node_border)) {
1033                                 copy_v2_v2(t->tsnap.snapPoint, loc);
1034                                 t->tsnap.snapNodeBorder = node_border;
1035
1036                                 t->tsnap.status |= POINT_INIT;
1037                         }
1038                         else {
1039                                 t->tsnap.status &= ~POINT_INIT;
1040                         }
1041                 }
1042         }
1043 }
1044
1045 /********************** TARGET **************************/
1046
1047 static void TargetSnapOffset(TransInfo *t, TransData *td)
1048 {
1049         if (t->spacetype == SPACE_NODE && td != NULL) {
1050                 bNode *node = td->extra;
1051                 char border = t->tsnap.snapNodeBorder;
1052                 float width  = BLI_rctf_size_x(&node->totr);
1053                 float height = BLI_rctf_size_y(&node->totr);
1054
1055 #ifdef USE_NODE_CENTER
1056                 if (border & NODE_LEFT)
1057                         t->tsnap.snapTarget[0] -= 0.5f * width;
1058                 if (border & NODE_RIGHT)
1059                         t->tsnap.snapTarget[0] += 0.5f * width;
1060                 if (border & NODE_BOTTOM)
1061                         t->tsnap.snapTarget[1] -= 0.5f * height;
1062                 if (border & NODE_TOP)
1063                         t->tsnap.snapTarget[1] += 0.5f * height;
1064 #else
1065                 if (border & NODE_LEFT)
1066                         t->tsnap.snapTarget[0] -= 0.0f;
1067                 if (border & NODE_RIGHT)
1068                         t->tsnap.snapTarget[0] += width;
1069                 if (border & NODE_BOTTOM)
1070                         t->tsnap.snapTarget[1] -= height;
1071                 if (border & NODE_TOP)
1072                         t->tsnap.snapTarget[1] += 0.0f;
1073 #endif
1074         }
1075 }
1076
1077 static void TargetSnapCenter(TransInfo *t)
1078 {
1079         /* Only need to calculate once */
1080         if ((t->tsnap.status & TARGET_INIT) == 0) {
1081                 copy_v3_v3(t->tsnap.snapTarget, t->center_global);
1082                 TargetSnapOffset(t, NULL);
1083
1084                 t->tsnap.status |= TARGET_INIT;
1085         }
1086 }
1087
1088 static void TargetSnapActive(TransInfo *t)
1089 {
1090         /* Only need to calculate once */
1091         if ((t->tsnap.status & TARGET_INIT) == 0) {
1092                 if (calculateCenterActive(t, true, t->tsnap.snapTarget)) {
1093                         TargetSnapOffset(t, NULL);
1094
1095                         t->tsnap.status |= TARGET_INIT;
1096                 }
1097                 /* No active, default to median */
1098                 else {
1099                         t->tsnap.target = SCE_SNAP_TARGET_MEDIAN;
1100                         t->tsnap.targetSnap = TargetSnapMedian;
1101                         TargetSnapMedian(t);
1102                 }
1103         }
1104 }
1105
1106 static void TargetSnapMedian(TransInfo *t)
1107 {
1108         // Only need to calculate once
1109         if ((t->tsnap.status & TARGET_INIT) == 0) {
1110                 int i_accum = 0;
1111
1112                 t->tsnap.snapTarget[0] = 0;
1113                 t->tsnap.snapTarget[1] = 0;
1114                 t->tsnap.snapTarget[2] = 0;
1115
1116                 FOREACH_TRANS_DATA_CONTAINER (t, tc) {
1117                         TransData *td = tc->data;
1118                         int i;
1119                         float v[3];
1120                         zero_v3(v);
1121
1122                         for (i = 0; i < tc->data_len && td->flag & TD_SELECTED; i++, td++) {
1123                                 add_v3_v3(v, td->center);
1124                         }
1125
1126                         if (tc->use_local_mat) {
1127                                 mul_m4_v3(tc->mat, v);
1128                         }
1129
1130                         add_v3_v3(t->tsnap.snapTarget, v);
1131                         i_accum += i;
1132                 }
1133
1134                 mul_v3_fl(t->tsnap.snapTarget, 1.0 / i_accum);
1135
1136                 TargetSnapOffset(t, NULL);
1137
1138                 t->tsnap.status |= TARGET_INIT;
1139         }
1140 }
1141
1142 static void TargetSnapClosest(TransInfo *t)
1143 {
1144         // Only valid if a snap point has been selected
1145         if (t->tsnap.status & POINT_INIT) {
1146                 float dist_closest = 0.0f;
1147                 TransData *closest = NULL;
1148
1149                 /* Object mode */
1150                 if (t->flag & T_OBJECT) {
1151                         int i;
1152                         FOREACH_TRANS_DATA_CONTAINER(t, tc) {
1153                                 TransData *td = tc->data;
1154                                 for (td = tc->data, i = 0; i < tc->data_len && td->flag & TD_SELECTED; i++, td++) {
1155                                         struct BoundBox *bb = BKE_object_boundbox_get(td->ob);
1156
1157                                         /* use boundbox if possible */
1158                                         if (bb) {
1159                                                 int j;
1160
1161                                                 for (j = 0; j < 8; j++) {
1162                                                         float loc[3];
1163                                                         float dist;
1164
1165                                                         copy_v3_v3(loc, bb->vec[j]);
1166                                                         mul_m4_v3(td->ext->obmat, loc);
1167
1168                                                         dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1169
1170                                                         if ((dist != TRANSFORM_DIST_INVALID) &&
1171                                                             (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1172                                                         {
1173                                                                 copy_v3_v3(t->tsnap.snapTarget, loc);
1174                                                                 closest = td;
1175                                                                 dist_closest = dist;
1176                                                         }
1177                                                 }
1178                                         }
1179                                         /* use element center otherwise */
1180                                         else {
1181                                                 float loc[3];
1182                                                 float dist;
1183
1184                                                 copy_v3_v3(loc, td->center);
1185
1186                                                 dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1187
1188                                                 if ((dist != TRANSFORM_DIST_INVALID) &&
1189                                                     (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1190                                                 {
1191                                                         copy_v3_v3(t->tsnap.snapTarget, loc);
1192                                                         closest = td;
1193                                                 }
1194                                         }
1195                                 }
1196                         }
1197                 }
1198                 else {
1199                         FOREACH_TRANS_DATA_CONTAINER(t, tc) {
1200                                 TransData *td = tc->data;
1201                                 int i;
1202                                 for (i = 0; i < tc->data_len && td->flag & TD_SELECTED; i++, td++) {
1203                                         float loc[3];
1204                                         float dist;
1205
1206                                         copy_v3_v3(loc, td->center);
1207
1208                                         if (tc->use_local_mat) {
1209                                                 mul_m4_v3(tc->mat, loc);
1210                                         }
1211
1212                                         dist = t->tsnap.distance(t, loc, t->tsnap.snapPoint);
1213
1214                                         if ((dist != TRANSFORM_DIST_INVALID) &&
1215                                             (closest == NULL || fabsf(dist) < fabsf(dist_closest)))
1216                                         {
1217                                                 copy_v3_v3(t->tsnap.snapTarget, loc);
1218                                                 closest = td;
1219                                                 dist_closest = dist;
1220                                         }
1221                                 }
1222                         }
1223                 }
1224
1225                 TargetSnapOffset(t, closest);
1226
1227                 t->tsnap.status |= TARGET_INIT;
1228         }
1229 }
1230
1231 bool snapObjectsTransform(
1232         TransInfo *t, const float mval[2],
1233         float *dist_px,
1234         float r_loc[3], float r_no[3])
1235 {
1236         return ED_transform_snap_object_project_view3d(
1237                 t->tsnap.object_context,
1238                 t->scene->toolsettings->snap_mode,
1239                 &(const struct SnapObjectParams){
1240                     .snap_select = t->tsnap.modeSelect,
1241                     .use_object_edit_cage = (t->flag & T_EDIT) != 0,
1242                     .use_occlusion_test = t->scene->toolsettings->snap_mode != SCE_SNAP_MODE_FACE,
1243                 },
1244                 mval, dist_px, r_loc, r_no);
1245 }
1246
1247
1248 /******************** PEELING *********************************/
1249
1250 bool peelObjectsSnapContext(
1251         SnapObjectContext *sctx,
1252         const float mval[2],
1253         const struct SnapObjectParams *params,
1254         const bool use_peel_object,
1255         /* return args */
1256         float r_loc[3], float r_no[3], float *r_thickness)
1257 {
1258         ListBase depths_peel = {0};
1259         ED_transform_snap_object_project_all_view3d_ex(
1260                 sctx,
1261                 params,
1262                 mval, -1.0f, false,
1263                 &depths_peel);
1264
1265         if (!BLI_listbase_is_empty(&depths_peel)) {
1266                 /* At the moment we only use the hits of the first object */
1267                 struct SnapObjectHitDepth *hit_min = depths_peel.first;
1268                 for (struct SnapObjectHitDepth *iter = hit_min->next; iter; iter = iter->next) {
1269                         if (iter->depth < hit_min->depth) {
1270                                 hit_min = iter;
1271                         }
1272                 }
1273                 struct SnapObjectHitDepth *hit_max = NULL;
1274
1275                 if (use_peel_object) {
1276                         /* if peeling objects, take the first and last from each object */
1277                         hit_max = hit_min;
1278                         for (struct SnapObjectHitDepth *iter = depths_peel.first; iter; iter = iter->next) {
1279                                 if ((iter->depth > hit_max->depth) && (iter->ob_uuid == hit_min->ob_uuid)) {
1280                                         hit_max = iter;
1281                                 }
1282                         }
1283                 }
1284                 else {
1285                         /* otherwise, pair first with second and so on */
1286                         for (struct SnapObjectHitDepth *iter = depths_peel.first; iter; iter = iter->next) {
1287                                 if ((iter != hit_min) && (iter->ob_uuid == hit_min->ob_uuid)) {
1288                                         if (hit_max == NULL) {
1289                                                 hit_max = iter;
1290                                         }
1291                                         else if (iter->depth < hit_max->depth) {
1292                                                 hit_max = iter;
1293                                         }
1294                                 }
1295                         }
1296                         /* in this case has only one hit. treat as raycast */
1297                         if (hit_max == NULL) {
1298                                 hit_max = hit_min;
1299                         }
1300                 }
1301
1302                 mid_v3_v3v3(r_loc, hit_min->co, hit_max->co);
1303
1304                 if (r_thickness) {
1305                         *r_thickness = hit_max->depth - hit_min->depth;
1306                 }
1307
1308                 /* XXX, is there a correct normal in this case ???, for now just z up */
1309                 r_no[0] = 0.0;
1310                 r_no[1] = 0.0;
1311                 r_no[2] = 1.0;
1312
1313                 BLI_freelistN(&depths_peel);
1314                 return true;
1315         }
1316         return false;
1317 }
1318
1319 bool peelObjectsTransform(
1320         TransInfo *t,
1321         const float mval[2],
1322         const bool use_peel_object,
1323         /* return args */
1324         float r_loc[3], float r_no[3], float *r_thickness)
1325 {
1326         return peelObjectsSnapContext(
1327                 t->tsnap.object_context,
1328                 mval,
1329                 &(const struct SnapObjectParams){
1330                     .snap_select = t->tsnap.modeSelect,
1331                     .use_object_edit_cage = (t->flag & T_EDIT) != 0,
1332                 },
1333                 use_peel_object,
1334                 r_loc, r_no, r_thickness);
1335 }
1336
1337 /******************** NODES ***********************************/
1338
1339 static bool snapNodeTest(View2D *v2d, bNode *node, eSnapSelect snap_select)
1340 {
1341         /* node is use for snapping only if a) snap mode matches and b) node is inside the view */
1342         return ((snap_select == SNAP_NOT_SELECTED && !(node->flag & NODE_SELECT)) ||
1343                 (snap_select == SNAP_ALL          && !(node->flag & NODE_ACTIVE))) &&
1344                 (node->totr.xmin < v2d->cur.xmax && node->totr.xmax > v2d->cur.xmin &&
1345                  node->totr.ymin < v2d->cur.ymax && node->totr.ymax > v2d->cur.ymin);
1346 }
1347
1348 static NodeBorder snapNodeBorder(int snap_node_mode)
1349 {
1350         NodeBorder flag = 0;
1351         if (snap_node_mode & SCE_SNAP_MODE_NODE_X) {
1352                 flag |= NODE_LEFT | NODE_RIGHT;
1353         }
1354         if (snap_node_mode & SCE_SNAP_MODE_NODE_Y) {
1355                 flag |= NODE_TOP | NODE_BOTTOM;
1356         }
1357         return flag;
1358 }
1359
1360 static bool snapNode(
1361         ToolSettings *ts, SpaceNode *UNUSED(snode), ARegion *ar, bNode *node, const int mval[2],
1362         float r_loc[2], float *r_dist_px, char *r_node_border)
1363 {
1364         View2D *v2d = &ar->v2d;
1365         NodeBorder border = snapNodeBorder(ts->snap_node_mode);
1366         bool retval = false;
1367         rcti totr;
1368         int new_dist;
1369
1370         UI_view2d_view_to_region_rcti(v2d, &node->totr, &totr);
1371
1372         if (border & NODE_LEFT) {
1373                 new_dist = abs(totr.xmin - mval[0]);
1374                 if (new_dist < *r_dist_px) {
1375                         UI_view2d_region_to_view(v2d, totr.xmin, mval[1], &r_loc[0], &r_loc[1]);
1376                         *r_dist_px = new_dist;
1377                         *r_node_border = NODE_LEFT;
1378                         retval = true;
1379                 }
1380         }
1381
1382         if (border & NODE_RIGHT) {
1383                 new_dist = abs(totr.xmax - mval[0]);
1384                 if (new_dist < *r_dist_px) {
1385                         UI_view2d_region_to_view(v2d, totr.xmax, mval[1], &r_loc[0], &r_loc[1]);
1386                         *r_dist_px = new_dist;
1387                         *r_node_border = NODE_RIGHT;
1388                         retval = true;
1389                 }
1390         }
1391
1392         if (border & NODE_BOTTOM) {
1393                 new_dist = abs(totr.ymin - mval[1]);
1394                 if (new_dist < *r_dist_px) {
1395                         UI_view2d_region_to_view(v2d, mval[0], totr.ymin, &r_loc[0], &r_loc[1]);
1396                         *r_dist_px = new_dist;
1397                         *r_node_border = NODE_BOTTOM;
1398                         retval = true;
1399                 }
1400         }
1401
1402         if (border & NODE_TOP) {
1403                 new_dist = abs(totr.ymax - mval[1]);
1404                 if (new_dist < *r_dist_px) {
1405                         UI_view2d_region_to_view(v2d, mval[0], totr.ymax, &r_loc[0], &r_loc[1]);
1406                         *r_dist_px = new_dist;
1407                         *r_node_border = NODE_TOP;
1408                         retval = true;
1409                 }
1410         }
1411
1412         return retval;
1413 }
1414
1415 static bool snapNodes(
1416         ToolSettings *ts, SpaceNode *snode, ARegion *ar,
1417         const int mval[2], eSnapSelect snap_select,
1418         float r_loc[2], float *r_dist_px, char *r_node_border)
1419 {
1420         bNodeTree *ntree = snode->edittree;
1421         bNode *node;
1422         bool retval = false;
1423
1424         *r_node_border = 0;
1425
1426         for (node = ntree->nodes.first; node; node = node->next) {
1427                 if (snapNodeTest(&ar->v2d, node, snap_select)) {
1428                         retval |= snapNode(ts, snode, ar, node, mval, r_loc, r_dist_px, r_node_border);
1429                 }
1430         }
1431
1432         return retval;
1433 }
1434
1435 bool snapNodesTransform(
1436         TransInfo *t, const int mval[2],
1437         float r_loc[2], float *r_dist_px, char *r_node_border)
1438 {
1439         return snapNodes(
1440                 t->settings, t->sa->spacedata.first, t->ar, mval, t->tsnap.modeSelect,
1441                 r_loc, r_dist_px, r_node_border);
1442 }
1443
1444 /*================================================================*/
1445
1446 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action);
1447
1448
1449 void snapGridIncrementAction(TransInfo *t, float *val, GearsType action)
1450 {
1451         float fac[3];
1452
1453         fac[NO_GEARS]    = t->snap[0];
1454         fac[BIG_GEARS]   = t->snap[1];
1455         fac[SMALL_GEARS] = t->snap[2];
1456
1457         applyGridIncrement(t, val, t->idx_max, fac, action);
1458 }
1459
1460
1461 void snapGridIncrement(TransInfo *t, float *val)
1462 {
1463         GearsType action;
1464
1465         /* only do something if using absolute or incremental grid snapping
1466          * and there is no valid snap point */
1467         if ((!(t->tsnap.mode & (SCE_SNAP_MODE_INCREMENT | SCE_SNAP_MODE_GRID)) ||
1468             validSnap(t)) && !doForceIncrementSnap(t))
1469         {
1470                 return;
1471         }
1472
1473         action = activeSnap(t) ? BIG_GEARS : NO_GEARS;
1474
1475         if (action == BIG_GEARS && (t->modifiers & MOD_PRECISION)) {
1476                 action = SMALL_GEARS;
1477         }
1478
1479         snapGridIncrementAction(t, val, action);
1480 }
1481
1482 void snapSequenceBounds(TransInfo *t, const int mval[2])
1483 {
1484         float xmouse, ymouse;
1485         int frame;
1486         int mframe;
1487         TransSeq *ts = TRANS_DATA_CONTAINER_FIRST_SINGLE(t)->custom.type.data;
1488         /* reuse increment, strictly speaking could be another snap mode, but leave as is */
1489         if (!(t->modifiers & MOD_SNAP_INVERT))
1490                 return;
1491
1492         /* convert to frame range */
1493         UI_view2d_region_to_view(&t->ar->v2d, mval[0], mval[1], &xmouse, &ymouse);
1494         mframe = round_fl_to_int(xmouse);
1495         /* now find the closest sequence */
1496         frame = BKE_sequencer_find_next_prev_edit(t->scene, mframe, SEQ_SIDE_BOTH, true, false, true);
1497
1498         if (!ts->snap_left)
1499                 frame = frame - (ts->max - ts->min);
1500
1501         t->values[0] = frame - ts->min;
1502 }
1503
1504 static void applyGridIncrement(TransInfo *t, float *val, int max_index, const float fac[3], GearsType action)
1505 {
1506         float asp_local[3] = {1, 1, 1};
1507         const bool use_aspect = ELEM(t->mode, TFM_TRANSLATION);
1508         const float *asp = use_aspect ? t->aspect : asp_local;
1509         int i;
1510
1511         BLI_assert((t->tsnap.mode & (SCE_SNAP_MODE_INCREMENT | SCE_SNAP_MODE_GRID)) || doForceIncrementSnap(t));
1512         BLI_assert(max_index <= 2);
1513
1514         /* Early bailing out if no need to snap */
1515         if (fac[action] == 0.0f) {
1516                 return;
1517         }
1518
1519         if (use_aspect) {
1520                 /* custom aspect for fcurve */
1521                 if (t->spacetype == SPACE_GRAPH) {
1522                         View2D *v2d = &t->ar->v2d;
1523                         View2DGrid *grid;
1524                         SpaceGraph *sipo = t->sa->spacedata.first;
1525                         int unity = V2D_UNIT_VALUES;
1526                         int unitx = (sipo->flag & SIPO_DRAWTIME) ? V2D_UNIT_SECONDS : V2D_UNIT_FRAMESCALE;
1527
1528                         /* grid */
1529                         grid = UI_view2d_grid_calc(t->scene, v2d, unitx, V2D_GRID_NOCLAMP, unity, V2D_GRID_NOCLAMP, t->ar->winx, t->ar->winy);
1530
1531                         UI_view2d_grid_size(grid, &asp_local[0], &asp_local[1]);
1532                         UI_view2d_grid_free(grid);
1533
1534                         asp = asp_local;
1535                 }
1536         }
1537
1538         /* absolute snapping on grid based on global center */
1539         if ((t->tsnap.snap_spatial_grid) && (t->mode == TFM_TRANSLATION)) {
1540                 const float *center_global = t->center_global;
1541                 bool use_local_axis = false;
1542
1543                 /* use a fallback for cursor selection,
1544                  * this isn't useful as a global center for absolute grid snapping
1545                  * since its not based on the position of the selection. */
1546                 if (t->around == V3D_AROUND_CURSOR) {
1547                         const TransCenterData *cd = transformCenter_from_type(t, V3D_AROUND_CENTER_MEDIAN);
1548                         center_global = cd->global;
1549                 }
1550
1551                 if (t->con.mode & (CON_AXIS0 | CON_AXIS1 | CON_AXIS2)) {
1552                         use_local_axis = true;
1553                 }
1554
1555                 for (i = 0; i <= max_index; i++) {
1556                         /* do not let unconstrained axis jump to absolute grid increments */
1557                         if (!(t->con.mode & CON_APPLY) || t->con.mode & (CON_AXIS0 << i)) {
1558                                 const float iter_fac = fac[action] * asp[i];
1559
1560                                 if (use_local_axis) {
1561                                         float local_axis[3];
1562                                         float pos_on_axis[3];
1563
1564                                         copy_v3_v3(local_axis, t->con.mtx[i]);
1565                                         copy_v3_v3(pos_on_axis, t->con.mtx[i]);
1566
1567                                         /* amount of movement on axis from initial pos */
1568                                         mul_v3_fl(pos_on_axis, val[i]);
1569
1570                                         /* actual global position on axis */
1571                                         add_v3_v3(pos_on_axis, center_global);
1572
1573                                         float min_dist = INFINITY;
1574                                         for (int j = 0; j < 3; j++) {
1575                                                 if (fabs(local_axis[j]) < 0.01f) {
1576                                                         /* Ignore very small (normalized) axis changes */
1577                                                         continue;
1578                                                 }
1579
1580                                                 /* closest point on grid */
1581                                                 float grid_p = iter_fac * roundf(pos_on_axis[j] / iter_fac);
1582                                                 float dist_p = fabs((grid_p - pos_on_axis[j]) / local_axis[j]);
1583
1584                                                 /* The amount of distance needed to travel along the
1585                                                  * local axis to snap to the closest grid point */
1586                                                 /* in the global j axis direction */
1587                                                 float move_dist = (grid_p - center_global[j]) / local_axis[j];
1588
1589                                                 if (dist_p < min_dist) {
1590                                                         min_dist = dist_p;
1591                                                         val[i] = move_dist;
1592                                                 }
1593                                         }
1594                                 }
1595                                 else {
1596                                         val[i] = iter_fac * roundf((val[i]  + center_global[i]) / iter_fac) - center_global[i];
1597                                 }
1598                         }
1599                 }
1600         }
1601         else {
1602                 /* relative snapping in fixed increments */
1603                 for (i = 0; i <= max_index; i++) {
1604                         const float iter_fac = fac[action] * asp[i];
1605                         val[i] = iter_fac * roundf(val[i] / iter_fac);
1606                 }
1607         }
1608 }