Merging r57518 through r57545 from trunk into soc-20133-depsgraph_mt
[blender-staging.git] / source / blender / editors / transform / transform_conversions.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
19  * All rights reserved.
20  *
21  * The Original Code is: all of this file.
22  *
23  * Contributor(s): none yet.
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28 /** \file blender/editors/transform/transform_conversions.c
29  *  \ingroup edtransform
30  */
31
32
33 #ifndef WIN32
34 #include <unistd.h>
35 #else
36 #include <io.h>
37 #endif
38 #include <string.h>
39 #include <math.h>
40
41 #include "DNA_anim_types.h"
42 #include "DNA_armature_types.h"
43 #include "DNA_lattice_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meta_types.h"
46 #include "DNA_node_types.h"
47 #include "DNA_screen_types.h"
48 #include "DNA_space_types.h"
49 #include "DNA_sequence_types.h"
50 #include "DNA_view3d_types.h"
51 #include "DNA_constraint_types.h"
52 #include "DNA_scene_types.h"
53 #include "DNA_meshdata_types.h"
54 #include "DNA_gpencil_types.h"
55 #include "DNA_movieclip_types.h"
56 #include "DNA_mask_types.h"
57
58 #include "MEM_guardedalloc.h"
59
60 #include "BLI_math.h"
61 #include "BLI_blenlib.h"
62 #include "BLI_array.h"
63 #include "BLI_utildefines.h"
64 #include "BLI_smallhash.h"
65
66 #include "BKE_DerivedMesh.h"
67 #include "BKE_action.h"
68 #include "BKE_armature.h"
69 #include "BKE_bmesh.h"
70 #include "BKE_constraint.h"
71 #include "BKE_context.h"
72 #include "BKE_curve.h"
73 #include "BKE_depsgraph.h"
74 #include "BKE_fcurve.h"
75 #include "BKE_global.h"
76 #include "BKE_gpencil.h"
77 #include "BKE_key.h"
78 #include "BKE_main.h"
79 #include "BKE_mesh.h"
80 #include "BKE_modifier.h"
81 #include "BKE_movieclip.h"
82 #include "BKE_nla.h"
83 #include "BKE_node.h"
84 #include "BKE_object.h"
85 #include "BKE_particle.h"
86 #include "BKE_pointcache.h"
87 #include "BKE_report.h"
88 #include "BKE_rigidbody.h"
89 #include "BKE_scene.h"
90 #include "BKE_sequencer.h"
91 #include "BKE_editmesh.h"
92 #include "BKE_tracking.h"
93 #include "BKE_mask.h"
94 #include "BKE_lattice.h"
95
96 #include "BIK_api.h"
97
98 #include "ED_anim_api.h"
99 #include "ED_armature.h"
100 #include "ED_particle.h"
101 #include "ED_image.h"
102 #include "ED_keyframing.h"
103 #include "ED_keyframes_edit.h"
104 #include "ED_object.h"
105 #include "ED_markers.h"
106 #include "ED_mesh.h"
107 #include "ED_node.h"
108 #include "ED_types.h"
109 #include "ED_uvedit.h"
110 #include "ED_clip.h"
111 #include "ED_mask.h"
112 #include "ED_util.h"  /* for crazyspace correction */
113
114 #include "WM_api.h"  /* for WM_event_add_notifier to deal with stabilization nodes */
115 #include "WM_types.h"
116
117 #include "UI_view2d.h"
118 #include "UI_interface.h"
119
120 #include "RNA_access.h"
121
122 #include "transform.h"
123 #include "bmesh.h"
124
125 #include "BLI_sys_types.h" // for intptr_t support
126
127
128 /* local function prototype - for Object/Bone Constraints */
129 static bool constraints_list_needinv(TransInfo *t, ListBase *list);
130
131 /* ************************** Functions *************************** */
132
133 static int trans_data_compare_dist(const void *a, const void *b)
134 {
135         const TransData *td_a = (const TransData *)a;
136         const TransData *td_b = (const TransData *)b;
137
138         if      (td_a->dist < td_b->dist) return -1;
139         else if (td_a->dist > td_b->dist) return  1;
140         else                              return  0;
141 }
142
143 static int trans_data_compare_rdist(const void *a, const void *b)
144 {
145         const TransData *td_a = (const TransData *)a;
146         const TransData *td_b = (const TransData *)b;
147
148         if      (td_a->rdist < td_b->rdist) return -1;
149         else if (td_a->rdist > td_b->rdist) return  1;
150         else                                return  0;
151 }
152
153 void sort_trans_data_dist(TransInfo *t)
154 {
155         TransData *start = t->data;
156         int i;
157
158         for (i = 0; i < t->total && start->flag & TD_SELECTED; i++)
159                 start++;
160         
161         if (i < t->total) {
162                 if (t->flag & T_PROP_CONNECTED)
163                         qsort(start, t->total - i, sizeof(TransData), trans_data_compare_dist);
164                 else
165                         qsort(start, t->total - i, sizeof(TransData), trans_data_compare_rdist);
166         }
167 }
168
169 static void sort_trans_data(TransInfo *t)
170 {
171         TransData *sel, *unsel;
172         TransData temp;
173         unsel = t->data;
174         sel = t->data;
175         sel += t->total - 1;
176         while (sel > unsel) {
177                 while (unsel->flag & TD_SELECTED) {
178                         unsel++;
179                         if (unsel == sel) {
180                                 return;
181                         }
182                 }
183                 while (!(sel->flag & TD_SELECTED)) {
184                         sel--;
185                         if (unsel == sel) {
186                                 return;
187                         }
188                 }
189                 temp = *unsel;
190                 *unsel = *sel;
191                 *sel = temp;
192                 sel--;
193                 unsel++;
194         }
195 }
196
197 /* distance calculated from not-selected vertex to nearest selected vertex
198  * warning; this is loops inside loop, has minor N^2 issues, but by sorting list it is OK */
199 static void set_prop_dist(TransInfo *t, const bool with_dist)
200 {
201         TransData *tob;
202         int a;
203
204         for (a = 0, tob = t->data; a < t->total; a++, tob++) {
205
206                 tob->rdist = 0.0f; // init, it was mallocced
207
208                 if ((tob->flag & TD_SELECTED) == 0) {
209                         TransData *td;
210                         int i;
211                         float dist, vec[3];
212
213                         tob->rdist = -1.0f; // signal for next loop
214
215                         for (i = 0, td = t->data; i < t->total; i++, td++) {
216                                 if (td->flag & TD_SELECTED) {
217                                         sub_v3_v3v3(vec, tob->center, td->center);
218                                         mul_m3_v3(tob->mtx, vec);
219                                         dist = len_squared_v3(vec);
220                                         if ((tob->rdist == -1.0f) || (dist < (tob->rdist * tob->rdist))) {
221                                                 tob->rdist = sqrtf(dist);
222                                         }
223                                 }
224                                 else {
225                                         break;  /* by definition transdata has selected items in beginning */
226                                 }
227                         }
228                         if (with_dist) {
229                                 tob->dist = tob->rdist;
230                         }
231                 }
232         }
233 }
234
235 /* ************************** CONVERSIONS ************************* */
236
237 /* ********************* texture space ********* */
238
239 static void createTransTexspace(TransInfo *t)
240 {
241         Scene *scene = t->scene;
242         TransData *td;
243         Object *ob;
244         ID *id;
245         short *texflag;
246
247         ob = OBACT;
248
249         if (ob == NULL) { // Shouldn't logically happen, but still...
250                 t->total = 0;
251                 return;
252         }
253
254         id = ob->data;
255         if (id == NULL || !ELEM3(GS(id->name), ID_ME, ID_CU, ID_MB)) {
256                 t->total = 0;
257                 return;
258         }
259
260         t->total = 1;
261         td = t->data = MEM_callocN(sizeof(TransData), "TransTexspace");
262         td->ext = t->ext = MEM_callocN(sizeof(TransDataExtension), "TransTexspace");
263
264         td->flag = TD_SELECTED;
265         copy_v3_v3(td->center, ob->obmat[3]);
266         td->ob = ob;
267
268         copy_m3_m4(td->mtx, ob->obmat);
269         copy_m3_m4(td->axismtx, ob->obmat);
270         normalize_m3(td->axismtx);
271         pseudoinverse_m3_m3(td->smtx, td->mtx, PSEUDOINVERSE_EPSILON);
272
273         if (BKE_object_obdata_texspace_get(ob, &texflag, &td->loc, &td->ext->size, &td->ext->rot)) {
274                 ob->dtx |= OB_TEXSPACE;
275                 *texflag &= ~ME_AUTOSPACE;
276         }
277
278         copy_v3_v3(td->iloc, td->loc);
279         copy_v3_v3(td->ext->irot, td->ext->rot);
280         copy_v3_v3(td->ext->isize, td->ext->size);
281 }
282
283 /* ********************* edge (for crease) ***** */
284
285 static void createTransEdge(TransInfo *t)
286 {
287         BMEditMesh *em = BKE_editmesh_from_object(t->obedit);
288         TransData *td = NULL;
289         BMEdge *eed;
290         BMIter iter;
291         float mtx[3][3], smtx[3][3];
292         int count = 0, countsel = 0;
293         int propmode = t->flag & T_PROP_EDIT;
294         int cd_edge_float_offset;
295
296         BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
297                 if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN)) {
298                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) countsel++;
299                         if (propmode) count++;
300                 }
301         }
302
303         if (countsel == 0)
304                 return;
305
306         if (propmode) {
307                 t->total = count;
308         }
309         else {
310                 t->total = countsel;
311         }
312
313         td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransCrease");
314
315         copy_m3_m4(mtx, t->obedit->obmat);
316         pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
317
318         /* create data we need */
319         if (t->mode == TFM_BWEIGHT) {
320                 BM_mesh_cd_flag_ensure(em->bm, BKE_mesh_from_object(t->obedit), ME_CDFLAG_EDGE_BWEIGHT);
321                 cd_edge_float_offset = CustomData_get_offset(&em->bm->edata, CD_BWEIGHT);
322         }
323         else { //if (t->mode == TFM_CREASE) {
324                 BLI_assert(t->mode == TFM_CREASE);
325                 BM_mesh_cd_flag_ensure(em->bm, BKE_mesh_from_object(t->obedit), ME_CDFLAG_EDGE_CREASE);
326                 cd_edge_float_offset = CustomData_get_offset(&em->bm->edata, CD_CREASE);
327         }
328
329         BLI_assert(cd_edge_float_offset != -1);
330
331         BM_ITER_MESH (eed, &iter, em->bm, BM_EDGES_OF_MESH) {
332                 if (!BM_elem_flag_test(eed, BM_ELEM_HIDDEN) && (BM_elem_flag_test(eed, BM_ELEM_SELECT) || propmode)) {
333                         float *fl_ptr;
334                         /* need to set center for center calculations */
335                         mid_v3_v3v3(td->center, eed->v1->co, eed->v2->co);
336
337                         td->loc = NULL;
338                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT))
339                                 td->flag = TD_SELECTED;
340                         else
341                                 td->flag = 0;
342
343                         copy_m3_m3(td->smtx, smtx);
344                         copy_m3_m3(td->mtx, mtx);
345
346                         td->ext = NULL;
347
348                         fl_ptr = BM_ELEM_CD_GET_VOID_P(eed, cd_edge_float_offset);
349                         td->val  =  fl_ptr;
350                         td->ival = *fl_ptr;
351
352                         td++;
353                 }
354         }
355 }
356
357 /* ********************* pose mode ************* */
358
359 static bKinematicConstraint *has_targetless_ik(bPoseChannel *pchan)
360 {
361         bConstraint *con = pchan->constraints.first;
362
363         for (; con; con = con->next) {
364                 if (con->type == CONSTRAINT_TYPE_KINEMATIC && (con->enforce != 0.0f)) {
365                         bKinematicConstraint *data = con->data;
366
367                         if (data->tar == NULL)
368                                 return data;
369                         if (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0)
370                                 return data;
371                 }
372         }
373         return NULL;
374 }
375
376 static short apply_targetless_ik(Object *ob)
377 {
378         bPoseChannel *pchan, *parchan, *chanlist[256];
379         bKinematicConstraint *data;
380         int segcount, apply = 0;
381
382         /* now we got a difficult situation... we have to find the
383          * target-less IK pchans, and apply transformation to the all
384          * pchans that were in the chain */
385
386         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
387                 data = has_targetless_ik(pchan);
388                 if (data && (data->flag & CONSTRAINT_IK_AUTO)) {
389
390                         /* fill the array with the bones of the chain (armature.c does same, keep it synced) */
391                         segcount = 0;
392
393                         /* exclude tip from chain? */
394                         if (!(data->flag & CONSTRAINT_IK_TIP))
395                                 parchan = pchan->parent;
396                         else
397                                 parchan = pchan;
398
399                         /* Find the chain's root & count the segments needed */
400                         for (; parchan; parchan = parchan->parent) {
401                                 chanlist[segcount] = parchan;
402                                 segcount++;
403
404                                 if (segcount == data->rootbone || segcount > 255) break;  // 255 is weak
405                         }
406                         for (; segcount; segcount--) {
407                                 Bone *bone;
408                                 float rmat[4][4] /*, tmat[4][4], imat[4][4]*/;
409
410                                 /* pose_mat(b) = pose_mat(b-1) * offs_bone * channel * constraint * IK  */
411                                 /* we put in channel the entire result of rmat = (channel * constraint * IK) */
412                                 /* pose_mat(b) = pose_mat(b-1) * offs_bone * rmat  */
413                                 /* rmat = pose_mat(b) * inv(pose_mat(b-1) * offs_bone ) */
414
415                                 parchan = chanlist[segcount - 1];
416                                 bone = parchan->bone;
417                                 bone->flag |= BONE_TRANSFORM;   /* ensures it gets an auto key inserted */
418
419                                 BKE_armature_mat_pose_to_bone(parchan, parchan->pose_mat, rmat);
420
421                                 /* apply and decompose, doesn't work for constraints or non-uniform scale well */
422                                 {
423                                         float rmat3[3][3], qrmat[3][3], imat3[3][3], smat[3][3];
424                                         
425                                         copy_m3_m4(rmat3, rmat);
426                                         
427                                         /* rotation */
428                                         /* [#22409] is partially caused by this, as slight numeric error introduced during
429                                          * the solving process leads to locked-axis values changing. However, we cannot modify
430                                          * the values here, or else there are huge discrepancies between IK-solver (interactive)
431                                          * and applied poses.
432                                          */
433                                         if (parchan->rotmode > 0)
434                                                 mat3_to_eulO(parchan->eul, parchan->rotmode, rmat3);
435                                         else if (parchan->rotmode == ROT_MODE_AXISANGLE)
436                                                 mat3_to_axis_angle(parchan->rotAxis, &parchan->rotAngle, rmat3);
437                                         else
438                                                 mat3_to_quat(parchan->quat, rmat3);
439                                         
440                                         /* for size, remove rotation */
441                                         /* causes problems with some constraints (so apply only if needed) */
442                                         if (data->flag & CONSTRAINT_IK_STRETCH) {
443                                                 if (parchan->rotmode > 0)
444                                                         eulO_to_mat3(qrmat, parchan->eul, parchan->rotmode);
445                                                 else if (parchan->rotmode == ROT_MODE_AXISANGLE)
446                                                         axis_angle_to_mat3(qrmat, parchan->rotAxis, parchan->rotAngle);
447                                                 else
448                                                         quat_to_mat3(qrmat, parchan->quat);
449                                                 
450                                                 invert_m3_m3(imat3, qrmat);
451                                                 mul_m3_m3m3(smat, rmat3, imat3);
452                                                 mat3_to_size(parchan->size, smat);
453                                         }
454                                         
455                                         /* causes problems with some constraints (e.g. childof), so disable this */
456                                         /* as it is IK shouldn't affect location directly */
457                                         /* copy_v3_v3(parchan->loc, rmat[3]); */
458                                 }
459
460                         }
461
462                         apply = 1;
463                         data->flag &= ~CONSTRAINT_IK_AUTO;
464                 }
465         }
466
467         return apply;
468 }
469
470 static void add_pose_transdata(TransInfo *t, bPoseChannel *pchan, Object *ob, TransData *td)
471 {
472         Bone *bone = pchan->bone;
473         float pmat[3][3], omat[3][3];
474         float cmat[3][3], tmat[3][3];
475         float vec[3];
476
477         copy_v3_v3(vec, pchan->pose_mat[3]);
478         copy_v3_v3(td->center, vec);
479
480         td->ob = ob;
481         td->flag = TD_SELECTED;
482         if (bone->flag & BONE_HINGE_CHILD_TRANSFORM) {
483                 td->flag |= TD_NOCENTER;
484         }
485
486         if (bone->flag & BONE_TRANSFORM_CHILD) {
487                 td->flag |= TD_NOCENTER;
488                 td->flag |= TD_NO_LOC;
489         }
490
491         td->protectflag = pchan->protectflag;
492
493         td->loc = pchan->loc;
494         copy_v3_v3(td->iloc, pchan->loc);
495
496         td->ext->size = pchan->size;
497         copy_v3_v3(td->ext->isize, pchan->size);
498
499         if (pchan->rotmode > 0) {
500                 td->ext->rot = pchan->eul;
501                 td->ext->rotAxis = NULL;
502                 td->ext->rotAngle = NULL;
503                 td->ext->quat = NULL;
504                 
505                 copy_v3_v3(td->ext->irot, pchan->eul);
506         }
507         else if (pchan->rotmode == ROT_MODE_AXISANGLE) {
508                 td->ext->rot = NULL;
509                 td->ext->rotAxis = pchan->rotAxis;
510                 td->ext->rotAngle = &pchan->rotAngle;
511                 td->ext->quat = NULL;
512                 
513                 td->ext->irotAngle = pchan->rotAngle;
514                 copy_v3_v3(td->ext->irotAxis, pchan->rotAxis);
515         }
516         else {
517                 td->ext->rot = NULL;
518                 td->ext->rotAxis = NULL;
519                 td->ext->rotAngle = NULL;
520                 td->ext->quat = pchan->quat;
521                 
522                 copy_qt_qt(td->ext->iquat, pchan->quat);
523         }
524         td->ext->rotOrder = pchan->rotmode;
525
526
527         /* proper way to get parent transform + own transform + constraints transform */
528         copy_m3_m4(omat, ob->obmat);
529
530         /* New code, using "generic" BKE_pchan_to_pose_mat(). */
531         {
532                 float rotscale_mat[4][4], loc_mat[4][4];
533                 float rpmat[3][3];
534
535                 BKE_pchan_to_pose_mat(pchan, rotscale_mat, loc_mat);
536                 if (t->mode == TFM_TRANSLATION)
537                         copy_m3_m4(pmat, loc_mat);
538                 else
539                         copy_m3_m4(pmat, rotscale_mat);
540
541                 /* Grrr! Exceptional case: When translating pose bones that are either Hinge or NoLocal,
542                  * and want align snapping, we just need both loc_mat and rotscale_mat.
543                  * So simply always store rotscale mat in td->ext, and always use it to apply rotations...
544                  * Ugly to need such hacks! :/ */
545                 copy_m3_m4(rpmat, rotscale_mat);
546
547                 if (constraints_list_needinv(t, &pchan->constraints)) {
548                         copy_m3_m4(tmat, pchan->constinv);
549                         invert_m3_m3(cmat, tmat);
550                         mul_serie_m3(td->mtx, pmat, omat, cmat, NULL, NULL, NULL, NULL, NULL);
551                         mul_serie_m3(td->ext->r_mtx, rpmat, omat, cmat, NULL, NULL, NULL, NULL, NULL);
552                 }
553                 else {
554                         mul_serie_m3(td->mtx, pmat, omat, NULL, NULL, NULL, NULL, NULL, NULL);
555                         mul_serie_m3(td->ext->r_mtx, rpmat, omat, NULL, NULL, NULL, NULL, NULL, NULL);
556                 }
557                 invert_m3_m3(td->ext->r_smtx, td->ext->r_mtx);
558         }
559
560         pseudoinverse_m3_m3(td->smtx, td->mtx, PSEUDOINVERSE_EPSILON);
561
562         /* exceptional case: rotate the pose bone which also applies transformation
563          * when a parentless bone has BONE_NO_LOCAL_LOCATION [] */
564         if (!ELEM(t->mode, TFM_TRANSLATION, TFM_RESIZE) && (pchan->bone->flag & BONE_NO_LOCAL_LOCATION)) {
565                 if (pchan->parent) {
566                         /* same as td->smtx but without pchan->bone->bone_mat */
567                         td->flag |= TD_PBONE_LOCAL_MTX_C;
568                         mul_m3_m3m3(td->ext->l_smtx, pchan->bone->bone_mat, td->smtx);
569                 }
570                 else {
571                         td->flag |= TD_PBONE_LOCAL_MTX_P;
572                 }
573         }
574         
575         /* for axismat we use bone's own transform */
576         copy_m3_m4(pmat, pchan->pose_mat);
577         mul_m3_m3m3(td->axismtx, omat, pmat);
578         normalize_m3(td->axismtx);
579
580         if (t->mode == TFM_BONESIZE) {
581                 bArmature *arm = t->poseobj->data;
582
583                 if (arm->drawtype == ARM_ENVELOPE) {
584                         td->loc = NULL;
585                         td->val = &bone->dist;
586                         td->ival = bone->dist;
587                 }
588                 else {
589                         // abusive storage of scale in the loc pointer :)
590                         td->loc = &bone->xwidth;
591                         copy_v3_v3(td->iloc, td->loc);
592                         td->val = NULL;
593                 }
594         }
595
596         /* in this case we can do target-less IK grabbing */
597         if (t->mode == TFM_TRANSLATION) {
598                 bKinematicConstraint *data = has_targetless_ik(pchan);
599                 if (data) {
600                         if (data->flag & CONSTRAINT_IK_TIP) {
601                                 copy_v3_v3(data->grabtarget, pchan->pose_tail);
602                         }
603                         else {
604                                 copy_v3_v3(data->grabtarget, pchan->pose_head);
605                         }
606                         td->loc = data->grabtarget;
607                         copy_v3_v3(td->iloc, td->loc);
608                         data->flag |= CONSTRAINT_IK_AUTO;
609
610                         /* only object matrix correction */
611                         copy_m3_m3(td->mtx, omat);
612                         pseudoinverse_m3_m3(td->smtx, td->mtx, PSEUDOINVERSE_EPSILON);
613                 }
614         }
615
616         /* store reference to first constraint */
617         td->con = pchan->constraints.first;
618 }
619
620 static void bone_children_clear_transflag(int mode, short around, ListBase *lb)
621 {
622         Bone *bone = lb->first;
623
624         for (; bone; bone = bone->next) {
625                 if ((bone->flag & BONE_HINGE) && (bone->flag & BONE_CONNECTED)) {
626                         bone->flag |= BONE_HINGE_CHILD_TRANSFORM;
627                 }
628                 else if ((bone->flag & BONE_TRANSFORM) &&
629                          (mode == TFM_ROTATION || mode == TFM_TRACKBALL) &&
630                          (around == V3D_LOCAL))
631                 {
632                         bone->flag |= BONE_TRANSFORM_CHILD;
633                 }
634                 else {
635                         bone->flag &= ~BONE_TRANSFORM;
636                 }
637
638                 bone_children_clear_transflag(mode, around, &bone->childbase);
639         }
640 }
641
642 /* sets transform flags in the bones
643  * returns total number of bones with BONE_TRANSFORM */
644 int count_set_pose_transflags(int *out_mode, short around, Object *ob)
645 {
646         bArmature *arm = ob->data;
647         bPoseChannel *pchan;
648         Bone *bone;
649         int mode = *out_mode;
650         int hastranslation = 0;
651         int total = 0;
652
653         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
654                 bone = pchan->bone;
655                 if (PBONE_VISIBLE(arm, bone)) {
656                         if ((bone->flag & BONE_SELECTED))
657                                 bone->flag |= BONE_TRANSFORM;
658                         else
659                                 bone->flag &= ~BONE_TRANSFORM;
660                         
661                         bone->flag &= ~BONE_HINGE_CHILD_TRANSFORM;
662                         bone->flag &= ~BONE_TRANSFORM_CHILD;
663                 }
664                 else
665                         bone->flag &= ~BONE_TRANSFORM;
666         }
667
668         /* make sure no bone can be transformed when a parent is transformed */
669         /* since pchans are depsgraph sorted, the parents are in beginning of list */
670         if (mode != TFM_BONESIZE) {
671                 for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
672                         bone = pchan->bone;
673                         if (bone->flag & BONE_TRANSFORM)
674                                 bone_children_clear_transflag(mode, around, &bone->childbase);
675                 }
676         }
677         /* now count, and check if we have autoIK or have to switch from translate to rotate */
678         hastranslation = 0;
679
680         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
681                 bone = pchan->bone;
682                 if (bone->flag & BONE_TRANSFORM) {
683                         total++;
684                         
685                         if (mode == TFM_TRANSLATION) {
686                                 if (has_targetless_ik(pchan) == NULL) {
687                                         if (pchan->parent && (pchan->bone->flag & BONE_CONNECTED)) {
688                                                 if (pchan->bone->flag & BONE_HINGE_CHILD_TRANSFORM)
689                                                         hastranslation = 1;
690                                         }
691                                         else if ((pchan->protectflag & OB_LOCK_LOC) != OB_LOCK_LOC)
692                                                 hastranslation = 1;
693                                 }
694                                 else
695                                         hastranslation = 1;
696                         }
697                 }
698         }
699
700         /* if there are no translatable bones, do rotation */
701         if (mode == TFM_TRANSLATION && !hastranslation) {
702                 *out_mode = TFM_ROTATION;
703         }
704
705         return total;
706 }
707
708
709 /* -------- Auto-IK ---------- */
710
711 /* adjust pose-channel's auto-ik chainlen */
712 static void pchan_autoik_adjust(bPoseChannel *pchan, short chainlen)
713 {
714         bConstraint *con;
715
716         /* don't bother to search if no valid constraints */
717         if ((pchan->constflag & (PCHAN_HAS_IK | PCHAN_HAS_TARGET)) == 0)
718                 return;
719
720         /* check if pchan has ik-constraint */
721         for (con = pchan->constraints.first; con; con = con->next) {
722                 if (con->type == CONSTRAINT_TYPE_KINEMATIC && (con->enforce != 0.0f)) {
723                         bKinematicConstraint *data = con->data;
724                         
725                         /* only accept if a temporary one (for auto-ik) */
726                         if (data->flag & CONSTRAINT_IK_TEMP) {
727                                 /* chainlen is new chainlen, but is limited by maximum chainlen */
728                                 if ((chainlen == 0) || (chainlen > data->max_rootbone))
729                                         data->rootbone = data->max_rootbone;
730                                 else
731                                         data->rootbone = chainlen;
732                         }
733                 }
734         }
735 }
736
737 /* change the chain-length of auto-ik */
738 void transform_autoik_update(TransInfo *t, short mode)
739 {
740         short *chainlen = &t->settings->autoik_chainlen;
741         bPoseChannel *pchan;
742
743         /* mode determines what change to apply to chainlen */
744         if (mode == 1) {
745                 /* mode=1 is from WHEELMOUSEDOWN... increases len */
746                 (*chainlen)++;
747         }
748         else if (mode == -1) {
749                 /* mode==-1 is from WHEELMOUSEUP... decreases len */
750                 if (*chainlen > 0) (*chainlen)--;
751         }
752
753         /* sanity checks (don't assume t->poseobj is set, or that it is an armature) */
754         if (ELEM(NULL, t->poseobj, t->poseobj->pose))
755                 return;
756
757         /* apply to all pose-channels */
758         for (pchan = t->poseobj->pose->chanbase.first; pchan; pchan = pchan->next) {
759                 pchan_autoik_adjust(pchan, *chainlen);
760         }
761 }
762
763 /* frees temporal IKs */
764 static void pose_grab_with_ik_clear(Object *ob)
765 {
766         bKinematicConstraint *data;
767         bPoseChannel *pchan;
768         bConstraint *con, *next;
769
770         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
771                 /* clear all temporary lock flags */
772                 pchan->ikflag &= ~(BONE_IK_NO_XDOF_TEMP | BONE_IK_NO_YDOF_TEMP | BONE_IK_NO_ZDOF_TEMP);
773                 
774                 pchan->constflag &= ~(PCHAN_HAS_IK | PCHAN_HAS_TARGET);
775                 
776                 /* remove all temporary IK-constraints added */
777                 for (con = pchan->constraints.first; con; con = next) {
778                         next = con->next;
779                         if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
780                                 data = con->data;
781                                 if (data->flag & CONSTRAINT_IK_TEMP) {
782                                         /* iTaSC needs clear for removed constraints */
783                                         BIK_clear_data(ob->pose);
784
785                                         BLI_remlink(&pchan->constraints, con);
786                                         MEM_freeN(con->data);
787                                         MEM_freeN(con);
788                                         continue;
789                                 }
790                                 pchan->constflag |= PCHAN_HAS_IK;
791                                 if (data->tar == NULL || (data->tar->type == OB_ARMATURE && data->subtarget[0] == 0))
792                                         pchan->constflag |= PCHAN_HAS_TARGET;
793                         }
794                 }
795         }
796 }
797
798 /* adds the IK to pchan - returns if added */
799 static short pose_grab_with_ik_add(bPoseChannel *pchan)
800 {
801         bKinematicConstraint *targetless = NULL;
802         bKinematicConstraint *data;
803         bConstraint *con;
804
805         /* Sanity check */
806         if (pchan == NULL)
807                 return 0;
808
809         /* Rule: not if there's already an IK on this channel */
810         for (con = pchan->constraints.first; con; con = con->next) {
811                 if (con->type == CONSTRAINT_TYPE_KINEMATIC) {
812                         data = con->data;
813                         
814                         if (data->tar == NULL || (data->tar->type == OB_ARMATURE && data->subtarget[0] == '\0')) {
815                                 /* make reference to constraint to base things off later (if it's the last targetless constraint encountered) */
816                                 targetless = (bKinematicConstraint *)con->data;
817                                 
818                                 /* but, if this is a targetless IK, we make it auto anyway (for the children loop) */
819                                 if (con->enforce != 0.0f) {
820                                         data->flag |= CONSTRAINT_IK_AUTO;
821                                         
822                                         /* if no chain length has been specified, just make things obey standard rotation locks too */
823                                         if (data->rootbone == 0) {
824                                                 for (; pchan; pchan = pchan->parent) {
825                                                         /* here, we set ik-settings for bone from pchan->protectflag */
826                                                         // XXX: careful with quats/axis-angle rotations where we're locking 4d components
827                                                         if (pchan->protectflag & OB_LOCK_ROTX) pchan->ikflag |= BONE_IK_NO_XDOF_TEMP;
828                                                         if (pchan->protectflag & OB_LOCK_ROTY) pchan->ikflag |= BONE_IK_NO_YDOF_TEMP;
829                                                         if (pchan->protectflag & OB_LOCK_ROTZ) pchan->ikflag |= BONE_IK_NO_ZDOF_TEMP;
830                                                 }
831                                         }
832                                         
833                                         return 0; 
834                                 }
835                         }
836                         
837                         if ((con->flag & CONSTRAINT_DISABLE) == 0 && (con->enforce != 0.0f))
838                                 return 0;
839                 }
840         }
841
842         con = BKE_add_pose_constraint(NULL, pchan, "TempConstraint", CONSTRAINT_TYPE_KINEMATIC);
843         pchan->constflag |= (PCHAN_HAS_IK | PCHAN_HAS_TARGET);    /* for draw, but also for detecting while pose solving */
844         data = con->data;
845         if (targetless) {
846                 /* if exists, use values from last targetless (but disabled) IK-constraint as base */
847                 *data = *targetless;
848         }
849         else
850                 data->flag = CONSTRAINT_IK_TIP;
851         data->flag |= CONSTRAINT_IK_TEMP | CONSTRAINT_IK_AUTO | CONSTRAINT_IK_POS;
852         copy_v3_v3(data->grabtarget, pchan->pose_tail);
853         data->rootbone = 0; /* watch-it! has to be 0 here, since we're still on the same bone for the first time through the loop [#25885] */
854         
855         /* we only include bones that are part of a continual connected chain */
856         do {
857                 /* here, we set ik-settings for bone from pchan->protectflag */
858                 // XXX: careful with quats/axis-angle rotations where we're locking 4d components
859                 if (pchan->protectflag & OB_LOCK_ROTX) pchan->ikflag |= BONE_IK_NO_XDOF_TEMP;
860                 if (pchan->protectflag & OB_LOCK_ROTY) pchan->ikflag |= BONE_IK_NO_YDOF_TEMP;
861                 if (pchan->protectflag & OB_LOCK_ROTZ) pchan->ikflag |= BONE_IK_NO_ZDOF_TEMP;
862                 
863                 /* now we count this pchan as being included */
864                 data->rootbone++;
865                 
866                 /* continue to parent, but only if we're connected to it */
867                 if (pchan->bone->flag & BONE_CONNECTED)
868                         pchan = pchan->parent;
869                 else
870                         pchan = NULL;
871         } while (pchan);
872
873         /* make a copy of maximum chain-length */
874         data->max_rootbone = data->rootbone;
875
876         return 1;
877 }
878
879 /* bone is a candidate to get IK, but we don't do it if it has children connected */
880 static short pose_grab_with_ik_children(bPose *pose, Bone *bone)
881 {
882         Bone *bonec;
883         short wentdeeper = 0, added = 0;
884
885         /* go deeper if children & children are connected */
886         for (bonec = bone->childbase.first; bonec; bonec = bonec->next) {
887                 if (bonec->flag & BONE_CONNECTED) {
888                         wentdeeper = 1;
889                         added += pose_grab_with_ik_children(pose, bonec);
890                 }
891         }
892         if (wentdeeper == 0) {
893                 bPoseChannel *pchan = BKE_pose_channel_find_name(pose, bone->name);
894                 if (pchan)
895                         added += pose_grab_with_ik_add(pchan);
896         }
897
898         return added;
899 }
900
901 /* main call which adds temporal IK chains */
902 static short pose_grab_with_ik(Object *ob)
903 {
904         bArmature *arm;
905         bPoseChannel *pchan, *parent;
906         Bone *bonec;
907         short tot_ik = 0;
908
909         if ((ob == NULL) || (ob->pose == NULL) || (ob->mode & OB_MODE_POSE) == 0)
910                 return 0;
911
912         arm = ob->data;
913
914         /* Rule: allow multiple Bones (but they must be selected, and only one ik-solver per chain should get added) */
915         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
916                 if (pchan->bone->layer & arm->layer) {
917                         if (pchan->bone->flag & BONE_SELECTED) {
918                                 /* Rule: no IK for solitatry (unconnected) bones */
919                                 for (bonec = pchan->bone->childbase.first; bonec; bonec = bonec->next) {
920                                         if (bonec->flag & BONE_CONNECTED) {
921                                                 break;
922                                         }
923                                 }
924                                 if ((pchan->bone->flag & BONE_CONNECTED) == 0 && (bonec == NULL))
925                                         continue;
926
927                                 /* rule: if selected Bone is not a root bone, it gets a temporal IK */
928                                 if (pchan->parent) {
929                                         /* only adds if there's no IK yet (and no parent bone was selected) */
930                                         for (parent = pchan->parent; parent; parent = parent->parent) {
931                                                 if (parent->bone->flag & BONE_SELECTED)
932                                                         break;
933                                         }
934                                         if (parent == NULL)
935                                                 tot_ik += pose_grab_with_ik_add(pchan);
936                                 }
937                                 else {
938                                         /* rule: go over the children and add IK to the tips */
939                                         tot_ik += pose_grab_with_ik_children(ob->pose, pchan->bone);
940                                 }
941                         }
942                 }
943         }
944
945         /* iTaSC needs clear for new IK constraints */
946         if (tot_ik)
947                 BIK_clear_data(ob->pose);
948
949         return (tot_ik) ? 1 : 0;
950 }
951
952
953 /* only called with pose mode active object now */
954 static void createTransPose(TransInfo *t, Object *ob)
955 {
956         bArmature *arm;
957         bPoseChannel *pchan;
958         TransData *td;
959         TransDataExtension *tdx;
960         short ik_on = 0;
961         int i;
962
963         t->total = 0;
964
965         /* check validity of state */
966         arm = BKE_armature_from_object(ob);
967         if ((arm == NULL) || (ob->pose == NULL)) return;
968
969         if (arm->flag & ARM_RESTPOS) {
970                 if (ELEM(t->mode, TFM_DUMMY, TFM_BONESIZE) == 0) {
971                         // XXX use transform operator reports
972                         // BKE_report(op->reports, RPT_ERROR, "Cannot select linked when sync selection is enabled");
973                         return;
974                 }
975         }
976
977         /* do we need to add temporal IK chains? */
978         if ((arm->flag & ARM_AUTO_IK) && t->mode == TFM_TRANSLATION) {
979                 ik_on = pose_grab_with_ik(ob);
980                 if (ik_on) t->flag |= T_AUTOIK;
981         }
982
983         /* set flags and count total (warning, can change transform to rotate) */
984         t->total = count_set_pose_transflags(&t->mode, t->around, ob);
985
986         if (t->total == 0) return;
987
988         t->flag |= T_POSE;
989         t->poseobj = ob; /* we also allow non-active objects to be transformed, in weightpaint */
990
991         /* disable PET, its not usable in pose mode yet [#32444] */
992         t->flag &= ~(T_PROP_EDIT | T_PROP_CONNECTED);
993
994         /* init trans data */
995         td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransPoseBone");
996         tdx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension), "TransPoseBoneExt");
997         for (i = 0; i < t->total; i++, td++, tdx++) {
998                 td->ext = tdx;
999                 td->val = NULL;
1000         }
1001
1002         /* use pose channels to fill trans data */
1003         td = t->data;
1004         for (pchan = ob->pose->chanbase.first; pchan; pchan = pchan->next) {
1005                 if (pchan->bone->flag & BONE_TRANSFORM) {
1006                         add_pose_transdata(t, pchan, ob, td);
1007                         td++;
1008                 }
1009         }
1010
1011         if (td != (t->data + t->total)) {
1012                 // XXX use transform operator reports
1013                 // BKE_report(op->reports, RPT_DEBUG, "Bone selection count error");
1014         }
1015
1016         /* initialize initial auto=ik chainlen's? */
1017         if (ik_on) transform_autoik_update(t, 0);
1018 }
1019
1020 /* ********************* armature ************** */
1021
1022 static void createTransArmatureVerts(TransInfo *t)
1023 {
1024         EditBone *ebo;
1025         bArmature *arm = t->obedit->data;
1026         ListBase *edbo = arm->edbo;
1027         TransData *td;
1028         float mtx[3][3], smtx[3][3], delta[3], bonemat[3][3];
1029         
1030         /* special hack for envelope drawmode and scaling:
1031          *  to allow scaling the size of the envelope around single points,
1032          *      mode should become TFM_BONE_ENVELOPE in this case
1033          */
1034         // TODO: maybe we need a separate hotkey for it, but this is consistent with 2.4x for now
1035         if ((t->mode == TFM_RESIZE) && (arm->drawtype == ARM_ENVELOPE))
1036                 t->mode = TFM_BONE_ENVELOPE;
1037         
1038         t->total = 0;
1039         for (ebo = edbo->first; ebo; ebo = ebo->next) {
1040                 if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) {
1041                         if (t->mode == TFM_BONESIZE) {
1042                                 if (ebo->flag & BONE_SELECTED)
1043                                         t->total++;
1044                         }
1045                         else if (t->mode == TFM_BONE_ROLL) {
1046                                 if (ebo->flag & BONE_SELECTED)
1047                                         t->total++;
1048                         }
1049                         else {
1050                                 if (ebo->flag & BONE_TIPSEL)
1051                                         t->total++;
1052                                 if (ebo->flag & BONE_ROOTSEL)
1053                                         t->total++;
1054                         }
1055                 }
1056         }
1057
1058         if (!t->total) return;
1059
1060         copy_m3_m4(mtx, t->obedit->obmat);
1061         pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
1062
1063         td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransEditBone");
1064
1065         for (ebo = edbo->first; ebo; ebo = ebo->next) {
1066                 ebo->oldlength = ebo->length;   // length==0.0 on extrude, used for scaling radius of bone points
1067
1068                 if (EBONE_VISIBLE(arm, ebo) && !(ebo->flag & BONE_EDITMODE_LOCKED)) {
1069                         if (t->mode == TFM_BONE_ENVELOPE) {
1070                                 if (ebo->flag & BONE_ROOTSEL) {
1071                                         td->val = &ebo->rad_head;
1072                                         td->ival = *td->val;
1073
1074                                         copy_v3_v3(td->center, ebo->head);
1075                                         td->flag = TD_SELECTED;
1076
1077                                         copy_m3_m3(td->smtx, smtx);
1078                                         copy_m3_m3(td->mtx, mtx);
1079
1080                                         td->loc = NULL;
1081                                         td->ext = NULL;
1082                                         td->ob = t->obedit;
1083
1084                                         td++;
1085                                 }
1086                                 if (ebo->flag & BONE_TIPSEL) {
1087                                         td->val = &ebo->rad_tail;
1088                                         td->ival = *td->val;
1089                                         copy_v3_v3(td->center, ebo->tail);
1090                                         td->flag = TD_SELECTED;
1091
1092                                         copy_m3_m3(td->smtx, smtx);
1093                                         copy_m3_m3(td->mtx, mtx);
1094
1095                                         td->loc = NULL;
1096                                         td->ext = NULL;
1097                                         td->ob = t->obedit;
1098
1099                                         td++;
1100                                 }
1101
1102                         }
1103                         else if (t->mode == TFM_BONESIZE) {
1104                                 if (ebo->flag & BONE_SELECTED) {
1105                                         if (arm->drawtype == ARM_ENVELOPE) {
1106                                                 td->loc = NULL;
1107                                                 td->val = &ebo->dist;
1108                                                 td->ival = ebo->dist;
1109                                         }
1110                                         else {
1111                                                 // abusive storage of scale in the loc pointer :)
1112                                                 td->loc = &ebo->xwidth;
1113                                                 copy_v3_v3(td->iloc, td->loc);
1114                                                 td->val = NULL;
1115                                         }
1116                                         copy_v3_v3(td->center, ebo->head);
1117                                         td->flag = TD_SELECTED;
1118
1119                                         /* use local bone matrix */
1120                                         sub_v3_v3v3(delta, ebo->tail, ebo->head);
1121                                         vec_roll_to_mat3(delta, ebo->roll, bonemat);
1122                                         mul_m3_m3m3(td->mtx, mtx, bonemat);
1123                                         invert_m3_m3(td->smtx, td->mtx);
1124
1125                                         copy_m3_m3(td->axismtx, td->mtx);
1126                                         normalize_m3(td->axismtx);
1127
1128                                         td->ext = NULL;
1129                                         td->ob = t->obedit;
1130
1131                                         td++;
1132                                 }
1133                         }
1134                         else if (t->mode == TFM_BONE_ROLL) {
1135                                 if (ebo->flag & BONE_SELECTED) {
1136                                         td->loc = NULL;
1137                                         td->val = &(ebo->roll);
1138                                         td->ival = ebo->roll;
1139
1140                                         copy_v3_v3(td->center, ebo->head);
1141                                         td->flag = TD_SELECTED;
1142
1143                                         td->ext = NULL;
1144                                         td->ob = t->obedit;
1145
1146                                         td++;
1147                                 }
1148                         }
1149                         else {
1150                                 if (ebo->flag & BONE_TIPSEL) {
1151                                         copy_v3_v3(td->iloc, ebo->tail);
1152                                         copy_v3_v3(td->center, (t->around == V3D_LOCAL) ? ebo->head : td->iloc);
1153                                         td->loc = ebo->tail;
1154                                         td->flag = TD_SELECTED;
1155                                         if (ebo->flag & BONE_EDITMODE_LOCKED)
1156                                                 td->protectflag = OB_LOCK_LOC | OB_LOCK_ROT | OB_LOCK_SCALE;
1157
1158                                         copy_m3_m3(td->smtx, smtx);
1159                                         copy_m3_m3(td->mtx, mtx);
1160
1161                                         sub_v3_v3v3(delta, ebo->tail, ebo->head);
1162                                         vec_roll_to_mat3(delta, ebo->roll, td->axismtx);
1163
1164                                         if ((ebo->flag & BONE_ROOTSEL) == 0) {
1165                                                 td->extra = ebo;
1166                                         }
1167
1168                                         td->ext = NULL;
1169                                         td->val = NULL;
1170                                         td->ob = t->obedit;
1171
1172                                         td++;
1173                                 }
1174                                 if (ebo->flag & BONE_ROOTSEL) {
1175                                         copy_v3_v3(td->iloc, ebo->head);
1176                                         copy_v3_v3(td->center, td->iloc);
1177                                         td->loc = ebo->head;
1178                                         td->flag = TD_SELECTED;
1179                                         if (ebo->flag & BONE_EDITMODE_LOCKED)
1180                                                 td->protectflag = OB_LOCK_LOC | OB_LOCK_ROT | OB_LOCK_SCALE;
1181
1182                                         copy_m3_m3(td->smtx, smtx);
1183                                         copy_m3_m3(td->mtx, mtx);
1184
1185                                         sub_v3_v3v3(delta, ebo->tail, ebo->head);
1186                                         vec_roll_to_mat3(delta, ebo->roll, td->axismtx);
1187
1188                                         td->extra = ebo; /* to fix roll */
1189
1190                                         td->ext = NULL;
1191                                         td->val = NULL;
1192                                         td->ob = t->obedit;
1193
1194                                         td++;
1195                                 }
1196                         }
1197                 }
1198         }
1199 }
1200
1201 /* ********************* meta elements ********* */
1202
1203 static void createTransMBallVerts(TransInfo *t)
1204 {
1205         MetaBall *mb = (MetaBall *)t->obedit->data;
1206         MetaElem *ml;
1207         TransData *td;
1208         TransDataExtension *tx;
1209         float mtx[3][3], smtx[3][3];
1210         int count = 0, countsel = 0;
1211         int propmode = t->flag & T_PROP_EDIT;
1212
1213         /* count totals */
1214         for (ml = mb->editelems->first; ml; ml = ml->next) {
1215                 if (ml->flag & SELECT) countsel++;
1216                 if (propmode) count++;
1217         }
1218
1219         /* note: in prop mode we need at least 1 selected */
1220         if (countsel == 0) return;
1221
1222         if (propmode) t->total = count;
1223         else t->total = countsel;
1224
1225         td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(MBall EditMode)");
1226         tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension), "MetaElement_TransExtension");
1227
1228         copy_m3_m4(mtx, t->obedit->obmat);
1229         pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
1230
1231         for (ml = mb->editelems->first; ml; ml = ml->next) {
1232                 if (propmode || (ml->flag & SELECT)) {
1233                         td->loc = &ml->x;
1234                         copy_v3_v3(td->iloc, td->loc);
1235                         copy_v3_v3(td->center, td->loc);
1236
1237                         quat_to_mat3(td->axismtx, ml->quat);
1238
1239                         if (ml->flag & SELECT) td->flag = TD_SELECTED | TD_USEQUAT | TD_SINGLESIZE;
1240                         else td->flag = TD_USEQUAT;
1241
1242                         copy_m3_m3(td->smtx, smtx);
1243                         copy_m3_m3(td->mtx, mtx);
1244
1245                         td->ext = tx;
1246
1247                         /* Radius of MetaElem (mass of MetaElem influence) */
1248                         if (ml->flag & MB_SCALE_RAD) {
1249                                 td->val = &ml->rad;
1250                                 td->ival = ml->rad;
1251                         }
1252                         else {
1253                                 td->val = &ml->s;
1254                                 td->ival = ml->s;
1255                         }
1256
1257                         /* expx/expy/expz determine "shape" of some MetaElem types */
1258                         tx->size = &ml->expx;
1259                         tx->isize[0] = ml->expx;
1260                         tx->isize[1] = ml->expy;
1261                         tx->isize[2] = ml->expz;
1262
1263                         /* quat is used for rotation of MetaElem */
1264                         tx->quat = ml->quat;
1265                         copy_qt_qt(tx->iquat, ml->quat);
1266
1267                         tx->rot = NULL;
1268
1269                         td++;
1270                         tx++;
1271                 }
1272         }
1273 }
1274
1275 /* ********************* curve/surface ********* */
1276
1277 static void calc_distanceCurveVerts(TransData *head, TransData *tail)
1278 {
1279         TransData *td, *td_near = NULL;
1280         for (td = head; td <= tail; td++) {
1281                 if (td->flag & TD_SELECTED) {
1282                         td_near = td;
1283                         td->dist = 0.0f;
1284                 }
1285                 else if (td_near) {
1286                         float dist;
1287                         dist = len_v3v3(td_near->center, td->center);
1288                         if (dist < (td - 1)->dist) {
1289                                 td->dist = (td - 1)->dist;
1290                         }
1291                         else {
1292                                 td->dist = dist;
1293                         }
1294                 }
1295                 else {
1296                         td->dist = FLT_MAX;
1297                         td->flag |= TD_NOTCONNECTED;
1298                 }
1299         }
1300         td_near = NULL;
1301         for (td = tail; td >= head; td--) {
1302                 if (td->flag & TD_SELECTED) {
1303                         td_near = td;
1304                         td->dist = 0.0f;
1305                 }
1306                 else if (td_near) {
1307                         float dist;
1308                         dist = len_v3v3(td_near->center, td->center);
1309                         if (td->flag & TD_NOTCONNECTED || dist < td->dist || (td + 1)->dist < td->dist) {
1310                                 td->flag &= ~TD_NOTCONNECTED;
1311                                 if (dist < (td + 1)->dist) {
1312                                         td->dist = (td + 1)->dist;
1313                                 }
1314                                 else {
1315                                         td->dist = dist;
1316                                 }
1317                         }
1318                 }
1319         }
1320 }
1321
1322 /* Utility function for getting the handle data from bezier's */
1323 static TransDataCurveHandleFlags *initTransDataCurveHandles(TransData *td, struct BezTriple *bezt)
1324 {
1325         TransDataCurveHandleFlags *hdata;
1326         td->flag |= TD_BEZTRIPLE;
1327         hdata = td->hdata = MEM_mallocN(sizeof(TransDataCurveHandleFlags), "CuHandle Data");
1328         hdata->ih1 = bezt->h1;
1329         hdata->h1 = &bezt->h1;
1330         hdata->ih2 = bezt->h2; /* in case the second is not selected */
1331         hdata->h2 = &bezt->h2;
1332         return hdata;
1333 }
1334
1335 static void createTransCurveVerts(TransInfo *t)
1336 {
1337         Curve *cu = t->obedit->data;
1338         TransData *td = NULL;
1339         Nurb *nu;
1340         BezTriple *bezt;
1341         BPoint *bp;
1342         float mtx[3][3], smtx[3][3];
1343         int a;
1344         int count = 0, countsel = 0;
1345         int propmode = t->flag & T_PROP_EDIT;
1346         short hide_handles = (cu->drawflag & CU_HIDE_HANDLES);
1347         ListBase *nurbs;
1348
1349         /* to be sure */
1350         if (cu->editnurb == NULL) return;
1351
1352         /* count total of vertices, check identical as in 2nd loop for making transdata! */
1353         nurbs = BKE_curve_editNurbs_get(cu);
1354         for (nu = nurbs->first; nu; nu = nu->next) {
1355                 if (nu->type == CU_BEZIER) {
1356                         for (a = 0, bezt = nu->bezt; a < nu->pntsu; a++, bezt++) {
1357                                 if (bezt->hide == 0) {
1358                                         if (hide_handles) {
1359                                                 if (bezt->f2 & SELECT) countsel += 3;
1360                                                 if (propmode) count += 3;
1361                                         }
1362                                         else {
1363                                                 if (bezt->f1 & SELECT) countsel++;
1364                                                 if (bezt->f2 & SELECT) countsel++;
1365                                                 if (bezt->f3 & SELECT) countsel++;
1366                                                 if (propmode) count += 3;
1367                                         }
1368                                 }
1369                         }
1370                 }
1371                 else {
1372                         for (a = nu->pntsu * nu->pntsv, bp = nu->bp; a > 0; a--, bp++) {
1373                                 if (bp->hide == 0) {
1374                                         if (propmode) count++;
1375                                         if (bp->f1 & SELECT) countsel++;
1376                                 }
1377                         }
1378                 }
1379         }
1380         /* note: in prop mode we need at least 1 selected */
1381         if (countsel == 0) return;
1382
1383         if (propmode) t->total = count;
1384         else t->total = countsel;
1385         t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Curve EditMode)");
1386
1387         copy_m3_m4(mtx, t->obedit->obmat);
1388         pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
1389
1390         td = t->data;
1391         for (nu = nurbs->first; nu; nu = nu->next) {
1392                 if (nu->type == CU_BEZIER) {
1393                         TransData *head, *tail;
1394                         head = tail = td;
1395                         for (a = 0, bezt = nu->bezt; a < nu->pntsu; a++, bezt++) {
1396                                 if (bezt->hide == 0) {
1397                                         TransDataCurveHandleFlags *hdata = NULL;
1398
1399                                         if (propmode ||
1400                                             ((bezt->f2 & SELECT) && hide_handles) ||
1401                                             ((bezt->f1 & SELECT) && hide_handles == 0))
1402                                         {
1403                                                 copy_v3_v3(td->iloc, bezt->vec[0]);
1404                                                 td->loc = bezt->vec[0];
1405                                                 copy_v3_v3(td->center, bezt->vec[(hide_handles || bezt->f2 & SELECT) ? 1 : 0]);
1406                                                 if (hide_handles) {
1407                                                         if (bezt->f2 & SELECT) td->flag = TD_SELECTED;
1408                                                         else td->flag = 0;
1409                                                 }
1410                                                 else {
1411                                                         if (bezt->f1 & SELECT) td->flag = TD_SELECTED;
1412                                                         else td->flag = 0;
1413                                                 }
1414                                                 td->ext = NULL;
1415                                                 td->val = NULL;
1416
1417                                                 hdata = initTransDataCurveHandles(td, bezt);
1418
1419                                                 copy_m3_m3(td->smtx, smtx);
1420                                                 copy_m3_m3(td->mtx, mtx);
1421
1422                                                 td++;
1423                                                 count++;
1424                                                 tail++;
1425                                         }
1426
1427                                         /* This is the Curve Point, the other two are handles */
1428                                         if (propmode || (bezt->f2 & SELECT)) {
1429                                                 copy_v3_v3(td->iloc, bezt->vec[1]);
1430                                                 td->loc = bezt->vec[1];
1431                                                 copy_v3_v3(td->center, td->loc);
1432                                                 if (bezt->f2 & SELECT) td->flag = TD_SELECTED;
1433                                                 else td->flag = 0;
1434                                                 td->ext = NULL;
1435
1436                                                 if (t->mode == TFM_CURVE_SHRINKFATTEN) { /* || t->mode==TFM_RESIZE) {*/ /* TODO - make points scale */
1437                                                         td->val = &(bezt->radius);
1438                                                         td->ival = bezt->radius;
1439                                                 }
1440                                                 else if (t->mode == TFM_TILT) {
1441                                                         td->val = &(bezt->alfa);
1442                                                         td->ival = bezt->alfa;
1443                                                 }
1444                                                 else {
1445                                                         td->val = NULL;
1446                                                 }
1447
1448                                                 copy_m3_m3(td->smtx, smtx);
1449                                                 copy_m3_m3(td->mtx, mtx);
1450
1451                                                 if ((bezt->f1 & SELECT) == 0 && (bezt->f3 & SELECT) == 0)
1452                                                         /* If the middle is selected but the sides arnt, this is needed */
1453                                                         if (hdata == NULL) { /* if the handle was not saved by the previous handle */
1454                                                                 hdata = initTransDataCurveHandles(td, bezt);
1455                                                         }
1456
1457                                                 td++;
1458                                                 count++;
1459                                                 tail++;
1460                                         }
1461                                         if (propmode ||
1462                                             ((bezt->f2 & SELECT) && hide_handles) ||
1463                                             ((bezt->f3 & SELECT) && hide_handles == 0))
1464                                         {
1465                                                 copy_v3_v3(td->iloc, bezt->vec[2]);
1466                                                 td->loc = bezt->vec[2];
1467                                                 copy_v3_v3(td->center, bezt->vec[(hide_handles || bezt->f2 & SELECT) ? 1 : 2]);
1468                                                 if (hide_handles) {
1469                                                         if (bezt->f2 & SELECT) td->flag = TD_SELECTED;
1470                                                         else td->flag = 0;
1471                                                 }
1472                                                 else {
1473                                                         if (bezt->f3 & SELECT) td->flag = TD_SELECTED;
1474                                                         else td->flag = 0;
1475                                                 }
1476                                                 td->ext = NULL;
1477                                                 td->val = NULL;
1478
1479                                                 if (hdata == NULL) { /* if the handle was not saved by the previous handle */
1480                                                         hdata = initTransDataCurveHandles(td, bezt);
1481                                                 }
1482
1483                                                 copy_m3_m3(td->smtx, smtx);
1484                                                 copy_m3_m3(td->mtx, mtx);
1485
1486                                                 td++;
1487                                                 count++;
1488                                                 tail++;
1489                                         }
1490
1491                                         (void)hdata;  /* quiet warning */
1492                                 }
1493                                 else if (propmode && head != tail) {
1494                                         calc_distanceCurveVerts(head, tail - 1);
1495                                         head = tail;
1496                                 }
1497                         }
1498                         if (propmode && head != tail)
1499                                 calc_distanceCurveVerts(head, tail - 1);
1500
1501                         /* TODO - in the case of tilt and radius we can also avoid allocating the initTransDataCurveHandles
1502                          * but for now just don't change handle types */
1503                         if (ELEM(t->mode, TFM_CURVE_SHRINKFATTEN, TFM_TILT) == 0) {
1504                                 /* sets the handles based on their selection, do this after the data is copied to the TransData */
1505                                 BKE_nurb_handles_test(nu);
1506                         }
1507                 }
1508                 else {
1509                         TransData *head, *tail;
1510                         head = tail = td;
1511                         for (a = nu->pntsu * nu->pntsv, bp = nu->bp; a > 0; a--, bp++) {
1512                                 if (bp->hide == 0) {
1513                                         if (propmode || (bp->f1 & SELECT)) {
1514                                                 copy_v3_v3(td->iloc, bp->vec);
1515                                                 td->loc = bp->vec;
1516                                                 copy_v3_v3(td->center, td->loc);
1517                                                 if (bp->f1 & SELECT) td->flag = TD_SELECTED;
1518                                                 else td->flag = 0;
1519                                                 td->ext = NULL;
1520
1521                                                 if (t->mode == TFM_CURVE_SHRINKFATTEN || t->mode == TFM_RESIZE) {
1522                                                         td->val = &(bp->radius);
1523                                                         td->ival = bp->radius;
1524                                                 }
1525                                                 else {
1526                                                         td->val = &(bp->alfa);
1527                                                         td->ival = bp->alfa;
1528                                                 }
1529
1530                                                 copy_m3_m3(td->smtx, smtx);
1531                                                 copy_m3_m3(td->mtx, mtx);
1532
1533                                                 td++;
1534                                                 count++;
1535                                                 tail++;
1536                                         }
1537                                 }
1538                                 else if (propmode && head != tail) {
1539                                         calc_distanceCurveVerts(head, tail - 1);
1540                                         head = tail;
1541                                 }
1542                         }
1543                         if (propmode && head != tail)
1544                                 calc_distanceCurveVerts(head, tail - 1);
1545                 }
1546         }
1547 }
1548
1549 /* ********************* lattice *************** */
1550
1551 static void createTransLatticeVerts(TransInfo *t)
1552 {
1553         Lattice *latt = ((Lattice *)t->obedit->data)->editlatt->latt;
1554         TransData *td = NULL;
1555         BPoint *bp, *actbp = BKE_lattice_active_point_get(latt);
1556         float mtx[3][3], smtx[3][3];
1557         int a;
1558         int count = 0, countsel = 0;
1559         int propmode = t->flag & T_PROP_EDIT;
1560
1561         bp = latt->def;
1562         a  = latt->pntsu * latt->pntsv * latt->pntsw;
1563         while (a--) {
1564                 if (bp->hide == 0) {
1565                         if (bp->f1 & SELECT) countsel++;
1566                         if (propmode) count++;
1567                 }
1568                 bp++;
1569         }
1570
1571         /* note: in prop mode we need at least 1 selected */
1572         if (countsel == 0) return;
1573
1574         if (propmode) t->total = count;
1575         else t->total = countsel;
1576         t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Lattice EditMode)");
1577
1578         copy_m3_m4(mtx, t->obedit->obmat);
1579         pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
1580
1581         td = t->data;
1582         bp = latt->def;
1583         a  = latt->pntsu * latt->pntsv * latt->pntsw;
1584         while (a--) {
1585                 if (propmode || (bp->f1 & SELECT)) {
1586                         if (bp->hide == 0) {
1587                                 copy_v3_v3(td->iloc, bp->vec);
1588                                 td->loc = bp->vec;
1589                                 copy_v3_v3(td->center, td->loc);
1590                                 if (bp->f1 & SELECT) {
1591                                         td->flag = TD_SELECTED;
1592                                         if (actbp && bp == actbp) td->flag |= TD_ACTIVE;
1593                                 }
1594                                 else td->flag = 0;
1595                                 copy_m3_m3(td->smtx, smtx);
1596                                 copy_m3_m3(td->mtx, mtx);
1597
1598                                 td->ext = NULL;
1599                                 td->val = NULL;
1600
1601                                 td++;
1602                                 count++;
1603                         }
1604                 }
1605                 bp++;
1606         }
1607 }
1608
1609 /* ******************* particle edit **************** */
1610 static void createTransParticleVerts(bContext *C, TransInfo *t)
1611 {
1612         TransData *td = NULL;
1613         TransDataExtension *tx;
1614         Base *base = CTX_data_active_base(C);
1615         Object *ob = CTX_data_active_object(C);
1616         ParticleEditSettings *pset = PE_settings(t->scene);
1617         PTCacheEdit *edit = PE_get_current(t->scene, ob);
1618         ParticleSystem *psys = NULL;
1619         ParticleSystemModifierData *psmd = NULL;
1620         PTCacheEditPoint *point;
1621         PTCacheEditKey *key;
1622         float mat[4][4];
1623         int i, k, transformparticle;
1624         int count = 0, hasselected = 0;
1625         int propmode = t->flag & T_PROP_EDIT;
1626
1627         if (edit == NULL || t->settings->particle.selectmode == SCE_SELECT_PATH) return;
1628
1629         psys = edit->psys;
1630
1631         if (psys)
1632                 psmd = psys_get_modifier(ob, psys);
1633
1634         base->flag |= BA_HAS_RECALC_DATA;
1635
1636         for (i = 0, point = edit->points; i < edit->totpoint; i++, point++) {
1637                 point->flag &= ~PEP_TRANSFORM;
1638                 transformparticle = 0;
1639
1640                 if ((point->flag & PEP_HIDE) == 0) {
1641                         for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
1642                                 if ((key->flag & PEK_HIDE) == 0) {
1643                                         if (key->flag & PEK_SELECT) {
1644                                                 hasselected = 1;
1645                                                 transformparticle = 1;
1646                                         }
1647                                         else if (propmode)
1648                                                 transformparticle = 1;
1649                                 }
1650                         }
1651                 }
1652
1653                 if (transformparticle) {
1654                         count += point->totkey;
1655                         point->flag |= PEP_TRANSFORM;
1656                 }
1657         }
1658
1659         /* note: in prop mode we need at least 1 selected */
1660         if (hasselected == 0) return;
1661
1662         t->total = count;
1663         td = t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Particle Mode)");
1664
1665         if (t->mode == TFM_BAKE_TIME)
1666                 tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension), "Particle_TransExtension");
1667         else
1668                 tx = t->ext = NULL;
1669
1670         unit_m4(mat);
1671
1672         invert_m4_m4(ob->imat, ob->obmat);
1673
1674         for (i = 0, point = edit->points; i < edit->totpoint; i++, point++) {
1675                 TransData *head, *tail;
1676                 head = tail = td;
1677
1678                 if (!(point->flag & PEP_TRANSFORM)) continue;
1679
1680                 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR))
1681                         psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles + i, mat);
1682
1683                 for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
1684                         if (key->flag & PEK_USE_WCO) {
1685                                 copy_v3_v3(key->world_co, key->co);
1686                                 mul_m4_v3(mat, key->world_co);
1687                                 td->loc = key->world_co;
1688                         }
1689                         else
1690                                 td->loc = key->co;
1691
1692                         copy_v3_v3(td->iloc, td->loc);
1693                         copy_v3_v3(td->center, td->loc);
1694
1695                         if (key->flag & PEK_SELECT)
1696                                 td->flag |= TD_SELECTED;
1697                         else if (!propmode)
1698                                 td->flag |= TD_SKIP;
1699
1700                         unit_m3(td->mtx);
1701                         unit_m3(td->smtx);
1702
1703                         /* don't allow moving roots */
1704                         if (k == 0 && pset->flag & PE_LOCK_FIRST && (!psys || !(psys->flag & PSYS_GLOBAL_HAIR)))
1705                                 td->protectflag |= OB_LOCK_LOC;
1706
1707                         td->ob = ob;
1708                         td->ext = tx;
1709                         if (t->mode == TFM_BAKE_TIME) {
1710                                 td->val = key->time;
1711                                 td->ival = *(key->time);
1712                                 /* abuse size and quat for min/max values */
1713                                 td->flag |= TD_NO_EXT;
1714                                 if (k == 0) tx->size = NULL;
1715                                 else tx->size = (key - 1)->time;
1716
1717                                 if (k == point->totkey - 1) tx->quat = NULL;
1718                                 else tx->quat = (key + 1)->time;
1719                         }
1720
1721                         td++;
1722                         if (tx)
1723                                 tx++;
1724                         tail++;
1725                 }
1726                 if (propmode && head != tail)
1727                         calc_distanceCurveVerts(head, tail - 1);
1728         }
1729 }
1730
1731 void flushTransParticles(TransInfo *t)
1732 {
1733         Scene *scene = t->scene;
1734         Object *ob = OBACT;
1735         PTCacheEdit *edit = PE_get_current(scene, ob);
1736         ParticleSystem *psys = edit->psys;
1737         ParticleSystemModifierData *psmd = NULL;
1738         PTCacheEditPoint *point;
1739         PTCacheEditKey *key;
1740         TransData *td;
1741         float mat[4][4], imat[4][4], co[3];
1742         int i, k, propmode = t->flag & T_PROP_EDIT;
1743
1744         if (psys)
1745                 psmd = psys_get_modifier(ob, psys);
1746
1747         /* we do transform in world space, so flush world space position
1748          * back to particle local space (only for hair particles) */
1749         td = t->data;
1750         for (i = 0, point = edit->points; i < edit->totpoint; i++, point++, td++) {
1751                 if (!(point->flag & PEP_TRANSFORM)) continue;
1752
1753                 if (psys && !(psys->flag & PSYS_GLOBAL_HAIR)) {
1754                         psys_mat_hair_to_global(ob, psmd->dm, psys->part->from, psys->particles + i, mat);
1755                         invert_m4_m4(imat, mat);
1756
1757                         for (k = 0, key = point->keys; k < point->totkey; k++, key++) {
1758                                 copy_v3_v3(co, key->world_co);
1759                                 mul_m4_v3(imat, co);
1760
1761
1762                                 /* optimization for proportional edit */
1763                                 if (!propmode || !compare_v3v3(key->co, co, 0.0001f)) {
1764                                         copy_v3_v3(key->co, co);
1765                                         point->flag |= PEP_EDIT_RECALC;
1766                                 }
1767                         }
1768                 }
1769                 else
1770                         point->flag |= PEP_EDIT_RECALC;
1771         }
1772
1773         PE_update_object(scene, OBACT, 1);
1774 }
1775
1776 /* ********************* mesh ****************** */
1777
1778 static bool bmesh_test_dist_add(BMVert *v, BMVert *v_other,
1779                                 float *dists, const float *dists_prev,
1780                                 float mtx[3][3])
1781 {
1782         if ((BM_elem_flag_test(v_other, BM_ELEM_SELECT) == 0) &&
1783             (BM_elem_flag_test(v_other, BM_ELEM_HIDDEN) == 0))
1784         {
1785                 const int i = BM_elem_index_get(v);
1786                 const int i_other = BM_elem_index_get(v_other);
1787                 float vec[3];
1788                 float dist_other;
1789                 sub_v3_v3v3(vec, v->co, v_other->co);
1790                 mul_m3_v3(mtx, vec);
1791
1792                 dist_other = dists_prev[i] + len_v3(vec);
1793                 if (dist_other < dists[i_other]) {
1794                         dists[i_other] = dist_other;
1795                         return true;
1796                 }
1797         }
1798
1799         return false;
1800 }
1801
1802 static void editmesh_set_connectivity_distance(BMesh *bm, float mtx[3][3], float *dists)
1803 {
1804         /* need to be very careful of feedback loops here, store previous dist's to avoid feedback */
1805         float *dists_prev = MEM_mallocN(bm->totvert * sizeof(float), __func__);
1806
1807         BMVert **queue = MEM_mallocN(bm->totvert * sizeof(BMVert *), __func__);
1808         STACK_DECLARE(queue);
1809
1810         /* any BM_ELEM_TAG'd vertex is in 'queue_next', so we don't add in twice */
1811         BMVert **queue_next = MEM_mallocN(bm->totvert * sizeof(BMVert *), __func__);
1812         STACK_DECLARE(queue_next);
1813
1814         STACK_INIT(queue);
1815         STACK_INIT(queue_next);
1816
1817         {
1818                 BMIter viter;
1819                 BMVert *v;
1820                 int i;
1821
1822                 BM_ITER_MESH_INDEX (v, &viter, bm, BM_VERTS_OF_MESH, i) {
1823                         BM_elem_index_set(v, i); /* set_inline */
1824                         BM_elem_flag_disable(v, BM_ELEM_TAG);
1825
1826                         if (BM_elem_flag_test(v, BM_ELEM_SELECT) == 0 || BM_elem_flag_test(v, BM_ELEM_HIDDEN)) {
1827                                 dists[i] = FLT_MAX;
1828                         }
1829                         else {
1830                                 STACK_PUSH(queue, v);
1831
1832                                 dists[i] = 0.0f;
1833                         }
1834                 }
1835         }
1836
1837         do {
1838                 BMVert *v;
1839                 unsigned int i;
1840
1841                 memcpy(dists_prev, dists, sizeof(float) * bm->totvert);
1842
1843                 while ((v = STACK_POP(queue))) {
1844                         BMIter iter;
1845                         BMEdge *e;
1846                         BMLoop *l;
1847
1848                         /* connected edge-verts */
1849                         BM_ITER_ELEM (e, &iter, v, BM_EDGES_OF_VERT) {
1850                                 if (BM_elem_flag_test(e, BM_ELEM_HIDDEN) == 0) {
1851                                         BMVert *v_other = BM_edge_other_vert(e, v);
1852                                         if (bmesh_test_dist_add(v, v_other, dists, dists_prev, mtx)) {
1853                                                 if (BM_elem_flag_test(v_other, BM_ELEM_TAG) == 0) {
1854                                                         BM_elem_flag_enable(v_other, BM_ELEM_TAG);
1855                                                         STACK_PUSH(queue_next, v_other);
1856                                                 }
1857                                         }
1858                                 }
1859                         }
1860                         
1861                         /* connected face-verts (excluding adjacent verts) */
1862                         BM_ITER_ELEM (l, &iter, v, BM_LOOPS_OF_VERT) {
1863                                 if ((BM_elem_flag_test(l->f, BM_ELEM_HIDDEN) == 0) && (l->f->len > 3)) {
1864                                         BMLoop *l_end = l->prev;
1865                                         l = l->next->next;
1866                                         do {
1867                                                 BMVert *v_other = l->v;
1868                                                 if (bmesh_test_dist_add(v, v_other, dists, dists_prev, mtx)) {
1869                                                         if (BM_elem_flag_test(v_other, BM_ELEM_TAG) == 0) {
1870                                                                 BM_elem_flag_enable(v_other, BM_ELEM_TAG);
1871                                                                 STACK_PUSH(queue_next, v_other);
1872                                                         }
1873                                                 }
1874                                         } while ((l = l->next) != l_end);
1875                                 }
1876                         }
1877                 }
1878
1879                 /* clear for the next loop */
1880                 for (i = 0; i < STACK_SIZE(queue_next); i++) {
1881                         BM_elem_flag_disable(queue_next[i], BM_ELEM_TAG);
1882                 }
1883
1884                 STACK_SWAP(queue, queue_next);
1885
1886                 /* none should be tagged now since 'queue_next' is empty */
1887                 BLI_assert(BM_iter_mesh_count_flag(BM_VERTS_OF_MESH, bm, BM_ELEM_TAG, true) == 0);
1888
1889         } while (STACK_SIZE(queue));
1890
1891         STACK_FREE(queue);
1892         STACK_FREE(queue_next);
1893
1894         MEM_freeN(queue);
1895         MEM_freeN(queue_next);
1896         MEM_freeN(dists_prev);
1897 }
1898
1899 static BMElem *bm_vert_single_select_face(BMVert *eve)
1900 {
1901         BMElem *ele;
1902         BMIter iter;
1903
1904         BM_ITER_ELEM (ele, &iter, eve, BM_FACES_OF_VERT) {
1905                 if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
1906                         return ele;
1907                 }
1908         }
1909         return NULL;
1910 }
1911 static BMElem *bm_vert_single_select_edge(BMVert *eve)
1912 {
1913         BMElem *ele;
1914         BMIter iter;
1915
1916         BM_ITER_ELEM (ele, &iter, eve, BM_EDGES_OF_VERT) {
1917                 if (BM_elem_flag_test(ele, BM_ELEM_SELECT)) {
1918                         return ele;
1919                 }
1920         }
1921         return NULL;
1922 }
1923
1924 /* way to overwrite what data is edited with transform */
1925 static void VertsToTransData(TransInfo *t, TransData *td, TransDataExtension *tx,
1926                              BMEditMesh *em, BMVert *eve, float *bweight)
1927 {
1928         td->flag = 0;
1929         //if (key)
1930         //      td->loc = key->co;
1931         //else
1932         td->loc = eve->co;
1933         copy_v3_v3(td->iloc, td->loc);
1934         copy_v3_v3(td->center, td->loc);
1935
1936         if (t->around == V3D_LOCAL) {
1937                 BMElem *ele;
1938                 bool is_axismat_set = false;
1939
1940                 if (em->selectmode & (SCE_SELECT_FACE | SCE_SELECT_EDGE) &&
1941                     (ele = ((em->selectmode & SCE_SELECT_FACE) ?
1942                             bm_vert_single_select_face(eve) :
1943                             bm_vert_single_select_edge(eve))))
1944                 {
1945                         float normal[3], tangent[3];
1946
1947                         BMEditSelection ese;
1948                         ese.next = ese.prev = NULL;
1949                         ese.ele = ele;
1950                         ese.htype = ele->head.htype;
1951
1952                         BM_editselection_center(&ese, td->center);
1953                         BM_editselection_normal(&ese, normal);
1954                         BM_editselection_plane(&ese, tangent);
1955
1956                         if (createSpaceNormalTangent(td->axismtx, normal, tangent)) {
1957                                 is_axismat_set = true;
1958                         }
1959                 }
1960
1961                 /* for verts or fallback when createSpaceNormalTangent fails */
1962                 if (is_axismat_set == false) {
1963                         axis_dominant_v3_to_m3(td->axismtx, eve->no);
1964                         invert_m3(td->axismtx);
1965                 }
1966         }
1967         else {
1968                 /* Setting normals */
1969                 copy_v3_v3(td->axismtx[2], eve->no);
1970                 td->axismtx[0][0]        =
1971                     td->axismtx[0][1]    =
1972                     td->axismtx[0][2]    =
1973                     td->axismtx[1][0]    =
1974                     td->axismtx[1][1]    =
1975                     td->axismtx[1][2]    = 0.0f;
1976         }
1977
1978
1979         td->ext = NULL;
1980         td->val = NULL;
1981         td->extra = NULL;
1982         if (t->mode == TFM_BWEIGHT) {
1983                 td->val  =  bweight;
1984                 td->ival = *bweight;
1985         }
1986         else if (t->mode == TFM_SKIN_RESIZE) {
1987                 MVertSkin *vs = CustomData_bmesh_get(&em->bm->vdata,
1988                                                      eve->head.data,
1989                                                      CD_MVERT_SKIN);
1990                 /* skin node size */
1991                 td->ext = tx;
1992                 copy_v3_v3(tx->isize, vs->radius);
1993                 tx->size = vs->radius;
1994                 td->val = vs->radius;
1995         }
1996         else if (t->mode == TFM_SHRINKFATTEN) {
1997                 td->ext = tx;
1998                 tx->isize[0] = BM_vert_calc_shell_factor_ex(eve, BM_ELEM_SELECT);
1999         }
2000 }
2001
2002 static void createTransEditVerts(TransInfo *t)
2003 {
2004         ToolSettings *ts = t->scene->toolsettings;
2005         TransData *tob = NULL;
2006         TransDataExtension *tx = NULL;
2007         BMEditMesh *em = BKE_editmesh_from_object(t->obedit);
2008         BMesh *bm = em->bm;
2009         BMVert *eve;
2010         BMIter iter;
2011         BMVert *eve_act = NULL;
2012         float *mappedcos = NULL, *quats = NULL;
2013         float mtx[3][3], smtx[3][3], (*defmats)[3][3] = NULL, (*defcos)[3] = NULL;
2014         float *dists = NULL;
2015         int count = 0, countsel = 0, a, totleft;
2016         int propmode = (t->flag & T_PROP_EDIT) ? (t->flag & (T_PROP_EDIT | T_PROP_CONNECTED)) : 0;
2017         int mirror = 0;
2018         char *selstate = NULL;
2019         short selectmode = ts->selectmode;
2020         int cd_vert_bweight_offset = -1;
2021
2022         if (t->flag & T_MIRROR) {
2023                 EDBM_verts_mirror_cache_begin(em, false, true);
2024                 mirror = 1;
2025         }
2026
2027         /* edge slide forces edge select */
2028         if (t->mode == TFM_EDGE_SLIDE) {
2029                 selectmode = SCE_SELECT_EDGE;
2030         }
2031
2032         /* BMESH_TODO, writing into the index values is BAD!, means we cant
2033          * use the values for vertex mirror - campbell */
2034
2035         // transform now requires awareness for select mode, so we tag the f1 flags in verts
2036         if (selectmode & SCE_SELECT_VERTEX) {
2037                 BM_ITER_MESH (eve, &iter, bm, BM_VERTS_OF_MESH) {
2038                         BM_elem_flag_set(eve, BM_ELEM_TAG, BM_elem_flag_test(eve, BM_ELEM_SELECT));
2039                 }
2040         }
2041         else if (selectmode & SCE_SELECT_EDGE) {
2042                 BMEdge *eed;
2043
2044                 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2045                 for (; eve; eve = BM_iter_step(&iter)) BM_elem_flag_disable(eve, BM_ELEM_TAG);
2046
2047                 eed = BM_iter_new(&iter, bm, BM_EDGES_OF_MESH, NULL);
2048                 for (; eed; eed = BM_iter_step(&iter)) {
2049                         if (BM_elem_flag_test(eed, BM_ELEM_SELECT)) {
2050                                 BM_elem_flag_enable(eed->v1, BM_ELEM_TAG);
2051                                 BM_elem_flag_enable(eed->v2, BM_ELEM_TAG);
2052                         }
2053                 }
2054         }
2055         else {
2056                 BMFace *efa;
2057                 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2058                 for (; eve; eve = BM_iter_step(&iter)) BM_elem_flag_disable(eve, BM_ELEM_TAG);
2059
2060                 efa = BM_iter_new(&iter, bm, BM_FACES_OF_MESH, NULL);
2061                 for (; efa; efa = BM_iter_step(&iter)) {
2062                         if (BM_elem_flag_test(efa, BM_ELEM_SELECT)) {
2063                                 BMIter liter;
2064                                 BMLoop *l;
2065
2066                                 l = BM_iter_new(&liter, bm, BM_LOOPS_OF_FACE, efa);
2067                                 for (; l; l = BM_iter_step(&liter)) {
2068                                         BM_elem_flag_enable(l->v, BM_ELEM_TAG);
2069                                 }
2070                         }
2071                 }
2072         }
2073
2074         /* now we can count. we store selection state in selstate, since
2075          * get_crazy_mapped_editverts messes up the index state of the
2076          * verts*/
2077         selstate = MEM_callocN(sizeof(*selstate) * bm->totvert, __func__);
2078         eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2079         for (a = 0; eve; eve = BM_iter_step(&iter), a++) {
2080                 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
2081                         if (BM_elem_flag_test(eve, BM_ELEM_TAG)) {
2082                                 selstate[a] = 1;
2083                                 countsel++;
2084                         }
2085                         if (propmode) count++;
2086                 }
2087         }
2088
2089         /* note: in prop mode we need at least 1 selected */
2090         if (countsel == 0) {
2091                 goto cleanup;
2092         }
2093
2094         /* check active */
2095         if (em->bm->selected.last) {
2096                 BMEditSelection *ese = em->bm->selected.last;
2097                 if (ese->htype == BM_VERT) {
2098                         eve_act = (BMVert *)ese->ele;
2099                 }
2100         }
2101
2102         if (t->mode == TFM_BWEIGHT) {
2103                 BM_mesh_cd_flag_ensure(bm, BKE_mesh_from_object(t->obedit), ME_CDFLAG_VERT_BWEIGHT);
2104                 cd_vert_bweight_offset = CustomData_get_offset(&bm->vdata, CD_BWEIGHT);
2105         }
2106
2107         if (propmode) {
2108                 t->total = count;
2109
2110                 /* allocating scratch arrays */
2111                 if (propmode & T_PROP_CONNECTED)
2112                         dists = MEM_mallocN(em->bm->totvert * sizeof(float), "scratch nears");
2113         }
2114         else {
2115                 t->total = countsel;
2116         }
2117
2118         tob = t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(Mesh EditMode)");
2119         if (ELEM(t->mode, TFM_SKIN_RESIZE, TFM_SHRINKFATTEN)) {
2120                 /* warning, this is overkill, we only need 2 extra floats,
2121                  * but this stores loads of extra stuff, for TFM_SHRINKFATTEN its even more overkill
2122                  * since we may not use the 'alt' transform mode to maintain shell thickness,
2123                  * but with generic transform code its hard to lazy init vars */
2124                 tx = t->ext = MEM_callocN(t->total * sizeof(TransDataExtension),
2125                                           "TransObData ext");
2126         }
2127
2128         copy_m3_m4(mtx, t->obedit->obmat);
2129         /* we use a pseudoinverse so that when one of the axes is scaled to 0,
2130          * matrix inversion still works and we can still moving along the other */
2131         pseudoinverse_m3_m3(smtx, mtx, PSEUDOINVERSE_EPSILON);
2132
2133         if (propmode & T_PROP_CONNECTED) {
2134                 editmesh_set_connectivity_distance(em->bm, mtx, dists);
2135         }
2136
2137         /* detect CrazySpace [tm] */
2138         if (modifiers_getCageIndex(t->scene, t->obedit, NULL, 1) >= 0) {
2139                 if (modifiers_isCorrectableDeformed(t->obedit)) {
2140                         /* check if we can use deform matrices for modifier from the
2141                          * start up to stack, they are more accurate than quats */
2142                         totleft = editbmesh_get_first_deform_matrices(t->scene, t->obedit, em, &defmats, &defcos);
2143
2144                         /* if we still have more modifiers, also do crazyspace
2145                          * correction with quats, relative to the coordinates after
2146                          * the modifiers that support deform matrices (defcos) */
2147                         if (totleft > 0) {
2148                                 mappedcos = crazyspace_get_mapped_editverts(t->scene, t->obedit);
2149                                 quats = MEM_mallocN((t->total) * sizeof(float) * 4, "crazy quats");
2150                                 crazyspace_set_quats_editmesh(em, (float *)defcos, mappedcos, quats); /* BMESH_TODO, abuses vertex index, should use an int array */
2151                                 if (mappedcos)
2152                                         MEM_freeN(mappedcos);
2153                         }
2154
2155                         if (defcos)
2156                                 MEM_freeN(defcos);
2157                 }
2158         }
2159
2160         /* find out which half we do */
2161         if (mirror) {
2162                 eve = BM_iter_new(&iter, bm, BM_VERTS_OF_MESH, NULL);
2163                 for (a = 0; eve; eve = BM_iter_step(&iter), a++) {
2164                         if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN) && selstate[a] && eve->co[0] != 0.0f) {
2165                                 if (eve->co[0] < 0.0f) {
2166                                         t->mirror = -1;
2167                                         mirror = -1;
2168                                 }
2169                                 break;
2170                         }
2171                 }
2172         }
2173
2174         BM_ITER_MESH_INDEX (eve, &iter, bm, BM_VERTS_OF_MESH, a) {
2175                 if (!BM_elem_flag_test(eve, BM_ELEM_HIDDEN)) {
2176                         if (propmode || selstate[a]) {
2177                                 float *bweight = (cd_vert_bweight_offset != -1) ? BM_ELEM_CD_GET_VOID_P(eve, cd_vert_bweight_offset) : NULL;
2178                                 
2179                                 VertsToTransData(t, tob, tx, em, eve, bweight);
2180                                 if (tx)
2181                                         tx++;
2182
2183                                 /* selected */
2184                                 if (selstate[a]) tob->flag |= TD_SELECTED;
2185
2186                                 /* active */
2187                                 if (eve == eve_act) tob->flag |= TD_ACTIVE;
2188
2189                                 if (propmode) {
2190                                         if (propmode & T_PROP_CONNECTED) {
2191                                                 tob->dist = dists[a];
2192                                         }
2193                                         else {
2194                                                 tob->flag |= TD_NOTCONNECTED;
2195                                                 tob->dist = FLT_MAX;
2196                                         }
2197                                 }
2198
2199                                 /* CrazySpace */
2200                                 if (defmats || (quats && BM_elem_index_get(eve) != -1)) {
2201                                         float mat[3][3], qmat[3][3], imat[3][3];
2202
2203                                         /* use both or either quat and defmat correction */
2204                                         if (quats && BM_elem_index_get(eve) != -1) {
2205                                                 quat_to_mat3(qmat, quats + 4 * BM_elem_index_get(eve));
2206
2207                                                 if (defmats)
2208                                                         mul_serie_m3(mat, mtx, qmat, defmats[a],
2209                                                                      NULL, NULL, NULL, NULL, NULL);
2210                                                 else
2211                                                         mul_m3_m3m3(mat, mtx, qmat);
2212                                         }
2213                                         else
2214                                                 mul_m3_m3m3(mat, mtx, defmats[a]);
2215
2216                                         invert_m3_m3(imat, mat);
2217
2218                                         copy_m3_m3(tob->smtx, imat);
2219                                         copy_m3_m3(tob->mtx, mat);
2220                                 }
2221                                 else {
2222                                         copy_m3_m3(tob->smtx, smtx);
2223                                         copy_m3_m3(tob->mtx, mtx);
2224                                 }
2225
2226                                 /* Mirror? */
2227                                 if ((mirror > 0 && tob->iloc[0] > 0.0f) || (mirror < 0 && tob->iloc[0] < 0.0f)) {
2228                                         BMVert *vmir = EDBM_verts_mirror_get(em, eve); //t->obedit, em, eve, tob->iloc, a);
2229                                         if (vmir && vmir != eve) {
2230                                                 tob->extra = vmir;
2231                                         }
2232                                 }
2233                                 tob++;
2234                         }
2235                 }
2236         }
2237         
2238         if (mirror != 0) {
2239                 tob = t->data;
2240                 for (a = 0; a < t->total; a++, tob++) {
2241                         if (ABS(tob->loc[0]) <= 0.00001f) {
2242                                 tob->flag |= TD_MIRROR_EDGE;
2243                         }
2244                 }
2245         }
2246
2247 cleanup:
2248         /* crazy space free */
2249         if (quats)
2250                 MEM_freeN(quats);
2251         if (defmats)
2252                 MEM_freeN(defmats);
2253         if (dists)
2254                 MEM_freeN(dists);
2255         
2256         MEM_freeN(selstate);
2257
2258         if (t->flag & T_MIRROR) {
2259                 EDBM_verts_mirror_cache_end(em);
2260                 mirror = 1;
2261         }
2262 }
2263
2264 /* *** NODE EDITOR *** */
2265 void flushTransNodes(TransInfo *t)
2266 {
2267         int a;
2268         TransData *td;
2269         TransData2D *td2d;
2270
2271         /* flush to 2d vector from internally used 3d vector */
2272         for (a = 0, td = t->data, td2d = t->data2d; a < t->total; a++, td++, td2d++) {
2273                 bNode *node = td->extra;
2274                 float vec[2];
2275                 
2276                 /* weirdo - but the node system is a mix of free 2d elements and dpi sensitive UI */
2277                 add_v2_v2v2(vec, td2d->loc, td2d->ih1);
2278                 node->locx = vec[0] / UI_DPI_FAC;
2279                 node->locy = vec[1] / UI_DPI_FAC;
2280         }
2281         
2282         /* handle intersection with noodles */
2283         if (t->total == 1) {
2284                 ED_node_link_intersect_test(t->sa, 1);
2285         }
2286 }
2287
2288 /* *** SEQUENCE EDITOR *** */
2289
2290 /* commented _only_ because the meta may have animation data which
2291  * needs moving too [#28158] */
2292
2293 #define SEQ_TX_NESTED_METAS
2294
2295 void flushTransSeq(TransInfo *t)
2296 {
2297         ListBase *seqbasep = BKE_sequencer_editing_get(t->scene, FALSE)->seqbasep; /* Editing null check already done */
2298         int a, new_frame, old_start;
2299         TransData *td = NULL;
2300         TransData2D *td2d = NULL;
2301         TransDataSeq *tdsq = NULL;
2302         Sequence *seq;
2303
2304
2305
2306         /* prevent updating the same seq twice
2307          * if the transdata order is changed this will mess up
2308          * but so will TransDataSeq */
2309         Sequence *seq_prev = NULL;
2310
2311         /* flush to 2d vector from internally used 3d vector */
2312         for (a = 0, td = t->data, td2d = t->data2d; a < t->total; a++, td++, td2d++) {
2313                 tdsq = (TransDataSeq *)td->extra;
2314                 seq = tdsq->seq;
2315                 old_start = seq->start;
2316                 new_frame = (int)floor(td2d->loc[0] + 0.5f);
2317
2318                 switch (tdsq->sel_flag) {
2319                         case SELECT:
2320 #ifdef SEQ_TX_NESTED_METAS
2321                                 if ((seq->depth != 0 || BKE_sequence_tx_test(seq))) /* for meta's, their children move */
2322                                         seq->start = new_frame - tdsq->start_offset;
2323 #else
2324                                 if (seq->type != SEQ_TYPE_META && (seq->depth != 0 || seq_tx_test(seq))) /* for meta's, their children move */
2325                                         seq->start = new_frame - tdsq->start_offset;
2326 #endif
2327                                 if (seq->depth == 0) {
2328                                         seq->machine = (int)floor(td2d->loc[1] + 0.5f);
2329                                         CLAMP(seq->machine, 1, MAXSEQ);
2330                                 }
2331                                 break;
2332                         case SEQ_LEFTSEL: /* no vertical transform  */
2333                                 BKE_sequence_tx_set_final_left(seq, new_frame);
2334                                 BKE_sequence_tx_handle_xlimits(seq, tdsq->flag & SEQ_LEFTSEL, tdsq->flag & SEQ_RIGHTSEL);
2335                                 BKE_sequence_single_fix(seq); /* todo - move this into aftertrans update? - old seq tx needed it anyway */
2336                                 break;
2337                         case SEQ_RIGHTSEL: /* no vertical transform  */
2338                                 BKE_sequence_tx_set_final_right(seq, new_frame);
2339                                 BKE_sequence_tx_handle_xlimits(seq, tdsq->flag & SEQ_LEFTSEL, tdsq->flag & SEQ_RIGHTSEL);
2340                                 BKE_sequence_single_fix(seq); /* todo - move this into aftertrans update? - old seq tx needed it anyway */
2341                                 break;
2342                 }
2343
2344                 if (seq != seq_prev) {
2345                         if (seq->depth == 0) {
2346                                 /* Calculate this strip and all nested strips
2347                                  * children are ALWAYS transformed first
2348                                  * so we don't need to do this in another loop. */
2349                                 BKE_sequence_calc(t->scene, seq);
2350                         }
2351                         else {
2352                                 BKE_sequence_calc_disp(t->scene, seq);
2353                         }
2354
2355                         if (tdsq->sel_flag == SELECT)
2356                                 BKE_sequencer_offset_animdata(t->scene, seq, seq->start - old_start);
2357                 }
2358                 seq_prev = seq;
2359         }
2360
2361
2362         if (ELEM(t->mode, TFM_SEQ_SLIDE, TFM_TIME_TRANSLATE)) { /* originally TFM_TIME_EXTEND, transform changes */
2363                 /* Special annoying case here, need to calc metas with TFM_TIME_EXTEND only */
2364
2365                 /* calc all meta's then effects [#27953] */
2366                 for (seq = seqbasep->first; seq; seq = seq->next) {
2367                         if (seq->type == SEQ_TYPE_META && seq->flag & SELECT) {
2368                                 BKE_sequence_calc(t->scene, seq);
2369                         }
2370                 }
2371                 for (seq = seqbasep->first; seq; seq = seq->next) {
2372                         if (seq->seq1 || seq->seq2 || seq->seq3) {
2373                                 BKE_sequence_calc(t->scene, seq);
2374                         }
2375                 }
2376         }
2377
2378         /* need to do the overlap check in a new loop otherwise adjacent strips
2379          * will not be updated and we'll get false positives */
2380         seq_prev = NULL;
2381         for (a = 0, td = t->data, td2d = t->data2d; a < t->total; a++, td++, td2d++) {
2382
2383                 tdsq = (TransDataSeq *)td->extra;
2384                 seq = tdsq->seq;
2385
2386                 if (seq != seq_prev) {
2387                         if (seq->depth == 0) {
2388                                 /* test overlap, displayes red outline */
2389                                 seq->flag &= ~SEQ_OVERLAP;
2390                                 if (BKE_sequence_test_overlap(seqbasep, seq)) {
2391                                         seq->flag |= SEQ_OVERLAP;
2392                                 }
2393                         }
2394                 }
2395                 seq_prev = seq;
2396         }
2397 }
2398
2399 /* ********************* UV ****************** */
2400
2401 static void UVsToTransData(SpaceImage *sima, TransData *td, TransData2D *td2d, float *uv, int selected)
2402 {
2403         float aspx, aspy;
2404
2405         ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
2406
2407         /* uv coords are scaled by aspects. this is needed for rotations and
2408          * proportional editing to be consistent with the stretched uv coords
2409          * that are displayed. this also means that for display and numinput,
2410          * and when the the uv coords are flushed, these are converted each time */
2411         td2d->loc[0] = uv[0] * aspx;
2412         td2d->loc[1] = uv[1] * aspy;
2413         td2d->loc[2] = 0.0f;
2414         td2d->loc2d = uv;
2415
2416         td->flag = 0;
2417         td->loc = td2d->loc;
2418         copy_v3_v3(td->center, td->loc);
2419         copy_v3_v3(td->iloc, td->loc);
2420
2421         memset(td->axismtx, 0, sizeof(td->axismtx));
2422         td->axismtx[2][2] = 1.0f;
2423
2424         td->ext = NULL; td->val = NULL;
2425
2426         if (selected) {
2427                 td->flag |= TD_SELECTED;
2428                 td->dist = 0.0;
2429         }
2430         else {
2431                 td->dist = FLT_MAX;
2432         }
2433         unit_m3(td->mtx);
2434         unit_m3(td->smtx);
2435 }
2436
2437 static void createTransUVs(bContext *C, TransInfo *t)
2438 {
2439         SpaceImage *sima = CTX_wm_space_image(C);
2440         Image *ima = CTX_data_edit_image(C);
2441         Scene *scene = t->scene;
2442         ToolSettings *ts = CTX_data_tool_settings(C);
2443         TransData *td = NULL;
2444         TransData2D *td2d = NULL;
2445         MTexPoly *tf;
2446         MLoopUV *luv;
2447         BMEditMesh *em = BKE_editmesh_from_object(t->obedit);
2448         BMFace *efa;
2449         BMLoop *l;
2450         BMIter iter, liter;
2451         UvElementMap *elementmap = NULL;
2452         char *island_enabled = NULL;
2453         int count = 0, countsel = 0, count_rejected = 0;
2454         int propmode = t->flag & T_PROP_EDIT;
2455         int propconnected = t->flag & T_PROP_CONNECTED;
2456
2457         const int cd_loop_uv_offset = CustomData_get_offset(&em->bm->ldata, CD_MLOOPUV);
2458
2459         if (!ED_space_image_show_uvedit(sima, t->obedit)) return;
2460
2461         /* count */
2462         if (propconnected) {
2463                 /* create element map with island information */
2464                 if (ts->uv_flag & UV_SYNC_SELECTION) {
2465                         elementmap = EDBM_uv_element_map_create(em, false, true);
2466                 }
2467                 else {
2468                         elementmap = EDBM_uv_element_map_create(em, true, true);
2469                 }
2470                 island_enabled = MEM_callocN(sizeof(*island_enabled) * elementmap->totalIslands, "TransIslandData(UV Editing)");
2471         }
2472
2473         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2474                 tf = CustomData_bmesh_get(&em->bm->pdata, efa->head.data, CD_MTEXPOLY);
2475
2476                 if (!uvedit_face_visible_test(scene, ima, efa, tf)) {
2477                         BM_elem_flag_disable(efa, BM_ELEM_TAG);
2478                         continue;
2479                 }
2480
2481                 BM_elem_flag_enable(efa, BM_ELEM_TAG);
2482                 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2483                         if (uvedit_uv_select_test(scene, l, cd_loop_uv_offset)) {
2484                                 countsel++;
2485
2486                                 if (propconnected) {
2487                                         UvElement *element = ED_uv_element_get(elementmap, efa, l);
2488                                         island_enabled[element->island] = TRUE;
2489                                 }
2490
2491                         }
2492
2493                         if (propmode) {
2494                                 count++;
2495                         }
2496                 }
2497         }
2498
2499         /* note: in prop mode we need at least 1 selected */
2500         if (countsel == 0) return;
2501
2502         t->total = (propmode) ? count : countsel;
2503         t->data = MEM_callocN(t->total * sizeof(TransData), "TransObData(UV Editing)");
2504         /* for each 2d uv coord a 3d vector is allocated, so that they can be
2505          * treated just as if they were 3d verts */
2506         t->data2d = MEM_callocN(t->total * sizeof(TransData2D), "TransObData2D(UV Editing)");
2507
2508         if (sima->flag & SI_CLIP_UV)
2509                 t->flag |= T_CLIP_UV;
2510
2511         td = t->data;
2512         td2d = t->data2d;
2513
2514         BM_ITER_MESH (efa, &iter, em->bm, BM_FACES_OF_MESH) {
2515                 if (!BM_elem_flag_test(efa, BM_ELEM_TAG))
2516                         continue;
2517
2518                 BM_ITER_ELEM (l, &liter, efa, BM_LOOPS_OF_FACE) {
2519                         if (!propmode && !uvedit_uv_select_test(scene, l, cd_loop_uv_offset))
2520                                 continue;
2521
2522                         if (propconnected) {
2523                                 UvElement *element = ED_uv_element_get(elementmap, efa, l);
2524                                 if (!island_enabled[element->island]) {
2525                                         count_rejected++;
2526                                         continue;
2527                                 }
2528                         }
2529                         
2530                         luv = BM_ELEM_CD_GET_VOID_P(l, cd_loop_uv_offset);
2531                         UVsToTransData(sima, td++, td2d++, luv->uv, uvedit_uv_select_test(scene, l, cd_loop_uv_offset));
2532                 }
2533         }
2534
2535         if (propconnected) {
2536                 t->total -= count_rejected;
2537                 EDBM_uv_element_map_free(elementmap);
2538                 MEM_freeN(island_enabled);
2539         }
2540
2541         if (sima->flag & SI_LIVE_UNWRAP)
2542                 ED_uvedit_live_unwrap_begin(t->scene, t->obedit);
2543 }
2544
2545 void flushTransUVs(TransInfo *t)
2546 {
2547         SpaceImage *sima = t->sa->spacedata.first;
2548         TransData2D *td;
2549         int a, width, height;
2550         float aspx, aspy, invx, invy;
2551
2552         ED_space_image_get_uv_aspect(sima, &aspx, &aspy);
2553         ED_space_image_get_size(sima, &width, &height);
2554         invx = 1.0f / aspx;
2555         invy = 1.0f / aspy;
2556
2557         /* flush to 2d vector from internally used 3d vector */
2558         for (a = 0, td = t->data2d; a < t->total; a++, td++) {
2559                 td->loc2d[0] = td->loc[0] * invx;
2560                 td->loc2d[1] = td->loc[1] * invy;
2561
2562                 if ((sima->flag & SI_PIXELSNAP) && (t->state != TRANS_CANCEL)) {
2563                         td->loc2d[0] = (float)floor(width * td->loc2d[0] + 0.5f) / width;
2564                         td->loc2d[1] = (float)floor(height * td->loc2d[1] + 0.5f) / height;
2565                 }
2566         }
2567 }
2568
2569 bool clipUVTransform(TransInfo *t, float vec[2], const bool resize)
2570 {
2571         TransData *td;
2572         int a, clipx = 1, clipy = 1;
2573         float aspx, aspy, min[2], max[2];
2574
2575         ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
2576         min[0] = min[1] = 0.0f;
2577         max[0] = aspx; max[1] = aspy;
2578
2579         for (a = 0, td = t->data; a < t->total; a++, td++) {
2580                 minmax_v2v2_v2(min, max, td->loc);
2581         }
2582
2583         if (resize) {
2584                 if (min[0] < 0.0f && t->center[0] > 0.0f && t->center[0] < aspx * 0.5f)
2585                         vec[0] *= t->center[0] / (t->center[0] - min[0]);
2586                 else if (max[0] > aspx && t->center[0] < aspx)
2587                         vec[0] *= (t->center[0] - aspx) / (t->center[0] - max[0]);
2588                 else
2589                         clipx = 0;
2590
2591                 if (min[1] < 0.0f && t->center[1] > 0.0f && t->center[1] < aspy * 0.5f)
2592                         vec[1] *= t->center[1] / (t->center[1] - min[1]);
2593                 else if (max[1] > aspy && t->center[1] < aspy)
2594                         vec[1] *= (t->center[1] - aspy) / (t->center[1] - max[1]);
2595                 else
2596                         clipy = 0;
2597         }
2598         else {
2599                 if (min[0] < 0.0f)
2600                         vec[0] -= min[0];
2601                 else if (max[0] > aspx)
2602                         vec[0] -= max[0] - aspx;
2603                 else
2604                         clipx = 0;
2605
2606                 if (min[1] < 0.0f)
2607                         vec[1] -= min[1];
2608                 else if (max[1] > aspy)
2609                         vec[1] -= max[1] - aspy;
2610                 else
2611                         clipy = 0;
2612         }
2613
2614         return (clipx || clipy);
2615 }
2616
2617 void clipUVData(TransInfo *t)
2618 {
2619         TransData *td = NULL;
2620         int a;
2621         float aspx, aspy;
2622
2623         ED_space_image_get_uv_aspect(t->sa->spacedata.first, &aspx, &aspy);
2624
2625         for (a = 0, td = t->data; a < t->total; a++, td++) {
2626                 if (td->flag & TD_NOACTION)
2627                         break;
2628
2629                 if ((td->flag & TD_SKIP) || (!td->loc))
2630                         continue;
2631
2632                 td->loc[0] = min_ff(max_ff(0.0f, td->loc[0]), aspx);
2633                 td->loc[1] = min_ff(max_ff(0.0f, td->loc[1]), aspy);
2634         }
2635 }
2636
2637 /* ********************* ANIMATION EDITORS (GENERAL) ************************* */
2638
2639 /* This function tests if a point is on the "mouse" side of the cursor/frame-marking */
2640 static bool FrameOnMouseSide(char side, float frame, float cframe)
2641 {
2642         /* both sides, so it doesn't matter */
2643         if (side == 'B') return true;
2644
2645         /* only on the named side */
2646         if (side == 'R')
2647                 return (frame >= cframe);
2648         else
2649                 return (frame <= cframe);
2650 }
2651
2652 /* ********************* NLA EDITOR ************************* */
2653
2654 static void createTransNlaData(bContext *C, TransInfo *t)
2655 {
2656         Scene *scene = t->scene;
2657         SpaceNla *snla = NULL;
2658         TransData *td = NULL;
2659         TransDataNla *tdn = NULL;
2660         
2661         bAnimContext ac;
2662         ListBase anim_data = {NULL, NULL};
2663         bAnimListElem *ale;
2664         int filter;
2665         
2666         int count = 0;
2667         
2668         /* determine what type of data we are operating on */
2669         if (ANIM_animdata_get_context(C, &ac) == 0)
2670                 return;
2671         snla = (SpaceNla *)ac.sl;
2672         
2673         /* filter data */
2674         filter = (ANIMFILTER_DATA_VISIBLE | ANIMFILTER_LIST_VISIBLE | ANIMFILTER_FOREDIT);
2675         ANIM_animdata_filter(&ac, &anim_data, filter, ac.data, ac.datatype);
2676         
2677         /* which side of the current frame should be allowed */
2678         if (t->mode == TFM_TIME_EXTEND) {
2679                 /* only side on which mouse is gets transformed */
2680                 float xmouse, ymouse;
2681                 
2682                 UI_view2d_region_to_view(&ac.ar->v2d, t->imval[0], t->imval[1], &xmouse, &ymouse);
2683                 t->frame_side = (xmouse > CFRA) ? 'R' : 'L';
2684         }
2685         else {
2686                 /* normal transform - both sides of current frame are considered */
2687                 t->frame_side = 'B';
2688         }
2689         
2690         /* loop 1: count how many strips are selected (consider each strip as 2 points) */
2691         for (ale = anim_data.first; ale; ale = ale->next) {
2692                 NlaTrack *nlt = (NlaTrack *)ale->data;
2693                 NlaStrip *strip;
2694                 
2695                 /* make some meta-strips for chains of selected strips */
2696                 BKE_nlastrips_make_metas(&nlt->strips, 1);
2697                 
2698                 /* only consider selected strips */
2699                 for (strip = nlt->strips.first; strip; strip = strip->next) {
2700                         // TODO: we can make strips have handles later on...
2701                         /* transition strips can't get directly transformed */
2702                         if (strip->type != NLASTRIP_TYPE_TRANSITION) {
2703                                 if (strip->flag & NLASTRIP_FLAG_SELECT) {
2704                                         if (FrameOnMouseSide(t->frame_side, strip->start, (float)CFRA)) count++;
2705                                         if (FrameOnMouseSide(t->frame_side, strip->end, (float)CFRA)) count++;
2706                                 }
2707                         }
2708                 }
2709         }
2710         
2711         /* stop if trying to build list if nothing selected */
2712         if (count == 0) {
2713                 /* clear temp metas that may have been created but aren't needed now 
2714                  * because they fell on the wrong side of CFRA
2715                  */
2716                 for (ale = anim_data.first; ale; ale = ale->next) {
2717                         NlaTrack *nlt = (NlaTrack *)ale->data;
2718                         BKE_nlastrips_clear_metas(&nlt->strips, 0, 1);
2719                 }
2720                 
2721                 /* cleanup temp list */
2722                 BLI_freelistN(&anim_data);
2723                 return;
2724         }
2725         
2726         /* allocate memory for data */
2727         t->total = count;
2728         
2729         t->data = MEM_callocN(t->total * sizeof(TransData), "TransData(NLA Editor)");
2730         td = t->data;
2731         t->customData = MEM_callocN(t->total * sizeof(TransDataNla), "TransDataNla (NLA Editor)");
2732         tdn = t->customData;
2733         t->flag |= T_FREE_CUSTOMDATA;
2734         
2735         /* loop 2: build transdata array */
2736         for (ale = anim_data.first; ale; ale = ale->next) {
2737                 /* only if a real NLA-track */
2738                 if (ale->type == ANIMTYPE_NLATRACK) {
2739                         AnimData *adt = ale->adt;
2740                         NlaTrack *nlt = (NlaTrack *)ale->data;
2741                         NlaStrip *strip;
2742                         
2743                         /* only consider selected strips */
2744                         for (strip = nlt->strips.first; strip; strip = strip->next) {
2745                                 // TODO: we can make strips have handles later on...
2746                                 /* transition strips can't get directly transformed */
2747                                 if (strip->type != NLASTRIP_TYPE_TRANSITION) {
2748                                         if (strip->flag & NLASTRIP_FLAG_SELECT) {
2749                                                 /* our transform data is constructed as follows:
2750                                                  *      - only the handles on the right side of the current-frame get included
2751                                                  *      - td structs are transform-elements operated on by the transform system
2752                                                  *        and represent a single handle. The storage/pointer used (val or loc) depends on
2753                                                  *        whether we're scaling or transforming. Ultimately though, the handles
2754                                                  *    the td writes to will simply be a dummy in tdn
2755                                                  *      - for each strip being transformed, a single tdn struct is used, so in some
2756                                                  *        cases, there will need to be 1 of these tdn elements in the array skipped...
2757                                                  */
2758                           &n