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