2.50: svn merge https://svn.blender.org/svnroot/bf-blender/trunk/blender -r17853...
[blender.git] / source / blender / editors / mesh / editface.c
1 /**
2  * $Id$
3  *
4  * ***** BEGIN GPL LICENSE BLOCK *****
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
19  *
20  * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
21  * All rights reserved.
22  *
23  * Contributor(s): Blender Foundation, Campbell Barton
24  *
25  * ***** END GPL LICENSE BLOCK *****
26  */
27
28
29 #include <math.h>
30 #include <string.h>
31
32 #include "MEM_guardedalloc.h"
33
34 #include "BLI_blenlib.h"
35 #include "BLI_arithb.h"
36 #include "BLI_heap.h"
37 #include "BLI_edgehash.h"
38 #include "BLI_editVert.h"
39
40 #include "IMB_imbuf_types.h"
41 #include "IMB_imbuf.h"
42
43 #include "DNA_image_types.h"
44 #include "DNA_mesh_types.h"
45 #include "DNA_meshdata_types.h"
46 #include "DNA_object_types.h"
47 #include "DNA_space_types.h"
48 #include "DNA_screen_types.h"
49 #include "DNA_scene_types.h"
50 #include "DNA_view3d_types.h"
51
52 #include "BKE_brush.h"
53 #include "BKE_customdata.h"
54 #include "BKE_depsgraph.h"
55 #include "BKE_DerivedMesh.h"
56 #include "BKE_displist.h"
57 #include "BKE_global.h"
58 #include "BKE_mesh.h"
59 #include "BKE_object.h"
60 #include "BKE_texture.h"
61 #include "BKE_utildefines.h"
62 #include "BKE_customdata.h"
63
64 #include "BIF_gl.h"
65 #include "BIF_glutil.h"
66
67 #include "GPU_draw.h"
68
69 #ifndef DISABLE_PYTHON
70 //#include "BPY_extern.h"
71 //#include "BPY_menus.h"
72 #endif
73
74 #include "ED_mesh.h"
75 #include "ED_object.h"
76 #include "ED_view3d.h"
77
78 #include "WM_api.h"
79 #include "WM_types.h"
80
81 /* own include */
82 #include "mesh_intern.h"
83
84
85 /* Pupmenu codes: */
86 #define UV_CUBE_MAPPING 2
87 #define UV_CYL_MAPPING 3
88 #define UV_SPHERE_MAPPING 4
89 #define UV_BOUNDS_MAPPING 5
90 #define UV_RESET_MAPPING 6
91 #define UV_WINDOW_MAPPING 7
92 #define UV_UNWRAP_MAPPING 8
93 #define UV_CYL_EX 32
94 #define UV_SPHERE_EX 34
95
96 /* Some macro tricks to make pupmenu construction look nicer :-)
97    Sorry, just did it for fun. */
98
99 #define _STR(x) " " #x
100 #define STRING(x) _STR(x)
101
102 #define MENUSTRING(string, code) string " %x" STRING(code)
103 #define MENUTITLE(string) string " %t|" 
104
105 /* ***************** XXX **************** */
106 static int sample_backbuf_rect() {return 0;}
107 static int sample_backbuf() {return 0;}
108 static void BIF_undo_push() {}
109 static void error() {}
110 static int pupmenu() {return 0;}
111 /* ***************** XXX **************** */
112
113
114 /* returns 0 if not found, otherwise 1 */
115 int facesel_face_pick(View3D *v3d, Mesh *me, short *mval, unsigned int *index, short rect)
116 {
117         if (!me || me->totface==0)
118                 return 0;
119
120         if (v3d->flag & V3D_NEEDBACKBUFDRAW) {
121 // XXX drawview.c!              check_backbuf();
122 // XXX          persp(PERSP_VIEW);
123         }
124
125         if (rect) {
126                 /* sample rect to increase changes of selecting, so that when clicking
127                    on an edge in the backbuf, we can still select a face */
128                 int dist;
129                 *index = sample_backbuf_rect(mval, 3, 1, me->totface+1, &dist,0,NULL);
130         }
131         else
132                 /* sample only on the exact position */
133                 *index = sample_backbuf(mval[0], mval[1]);
134
135         if ((*index)<=0 || (*index)>(unsigned int)me->totface)
136                 return 0;
137
138         (*index)--;
139         
140         return 1;
141 }
142
143 /* only operates on the edit object - this is all thats needed at the moment */
144 static void uv_calc_center_vector(Scene *scene, View3D *v3d, float *result, Object *ob, EditMesh *em)
145 {
146         float min[3], max[3], *cursx;
147         
148         EditFace *efa;
149         switch (v3d->around) 
150         {
151         case V3D_CENTER: /* bounding box center */
152                 min[0]= min[1]= min[2]= 1e20f;
153                 max[0]= max[1]= max[2]= -1e20f; 
154
155                 for (efa= em->faces.first; efa; efa= efa->next) {
156                         if (efa->f & SELECT) {
157                                 DO_MINMAX(efa->v1->co, min, max);
158                                 DO_MINMAX(efa->v2->co, min, max);
159                                 DO_MINMAX(efa->v3->co, min, max);
160                                 if(efa->v4) DO_MINMAX(efa->v4->co, min, max);
161                         }
162                 }
163                 VecMidf(result, min, max);
164                 break;
165         case V3D_CURSOR: /*cursor center*/ 
166                 cursx= give_cursor(scene, v3d);
167                 /* shift to objects world */
168                 result[0]= cursx[0]-ob->obmat[3][0];
169                 result[1]= cursx[1]-ob->obmat[3][1];
170                 result[2]= cursx[2]-ob->obmat[3][2];
171                 break;
172         case V3D_LOCAL: /*object center*/
173         case V3D_CENTROID: /* multiple objects centers, only one object here*/
174         default:
175                 result[0]= result[1]= result[2]= 0.0;
176                 break;
177         }
178 }
179
180 static void uv_calc_map_matrix(float result[][4], RegionView3D *rv3d, Object *ob, float upangledeg, float sideangledeg, float radius)
181 {
182         float rotup[4][4], rotside[4][4], viewmatrix[4][4], rotobj[4][4];
183         float sideangle= 0.0, upangle= 0.0;
184         int k;
185
186         /* get rotation of the current view matrix */
187         Mat4CpyMat4(viewmatrix, rv3d->viewmat);
188         /* but shifting */
189         for( k= 0; k< 4; k++) viewmatrix[3][k] =0.0;
190
191         /* get rotation of the current object matrix */
192         Mat4CpyMat4(rotobj,ob->obmat);
193         /* but shifting */
194         for( k= 0; k< 4; k++) rotobj[3][k] =0.0;
195
196         Mat4Clr(*rotup);
197         Mat4Clr(*rotside);
198
199         /* compensate front/side.. against opengl x,y,z world definition */
200         /* this is "kanonen gegen spatzen", a few plus minus 1 will do here */
201         /* i wanted to keep the reason here, so we're rotating*/
202         sideangle= M_PI * (sideangledeg + 180.0) /180.0;
203         rotside[0][0]= (float)cos(sideangle);
204         rotside[0][1]= -(float)sin(sideangle);
205         rotside[1][0]= (float)sin(sideangle);
206         rotside[1][1]= (float)cos(sideangle);
207         rotside[2][2]= 1.0f;
208       
209         upangle= M_PI * upangledeg /180.0;
210         rotup[1][1]= (float)cos(upangle)/radius;
211         rotup[1][2]= -(float)sin(upangle)/radius;
212         rotup[2][1]= (float)sin(upangle)/radius;
213         rotup[2][2]= (float)cos(upangle)/radius;
214         rotup[0][0]= (float)1.0/radius;
215
216         /* calculate transforms*/
217         Mat4MulSerie(result,rotup,rotside,viewmatrix,rotobj,NULL,NULL,NULL,NULL);
218 }
219
220 static void uv_calc_shift_project(ARegion *ar, View3D *v3d, float *target, float *shift, float rotmat[][4], int projectionmode, float *source, float *min, float *max)
221 {
222         RegionView3D *rv3d= ar->regiondata;
223         float pv[3];
224
225         VecSubf(pv, source, shift);
226         Mat4MulVecfl(rotmat, pv);
227
228         switch(projectionmode) {
229         case B_UVAUTO_CYLINDER: 
230                 tubemap(pv[0], pv[1], pv[2], &target[0],&target[1]);
231                 /* split line is always zero */
232                 if (target[0] >= 1.0f) target[0] -= 1.0f;  
233                 break;
234
235         case B_UVAUTO_SPHERE: 
236                 spheremap(pv[0], pv[1], pv[2], &target[0],&target[1]);
237                 /* split line is always zero */
238                 if (target[0] >= 1.0f) target[0] -= 1.0f;
239                 break;
240
241         case 3: /* ortho special case for BOUNDS */
242                 target[0] = -pv[0];
243                 target[1] = pv[2];
244                 break;
245
246         case 4: 
247                 {
248                 /* very special case for FROM WINDOW */
249                 float pv4[4], dx, dy, x= 0.0, y= 0.0;
250
251                 dx= ar->winx;
252                 dy= ar->winy;
253
254                 VecCopyf(pv4, source);
255         pv4[3] = 1.0;
256
257                 /* rotmat is the object matrix in this case */
258         Mat4MulVec4fl(rotmat, pv4); 
259
260                 /* almost project_short */
261             Mat4MulVec4fl(rv3d->persmat, pv4);
262                 if (fabs(pv4[3]) > 0.00001) { /* avoid division by zero */
263                         target[0] = dx/2.0 + (dx/2.0)*pv4[0]/pv4[3];
264                         target[1] = dy/2.0 + (dy/2.0)*pv4[1]/pv4[3];
265                 }
266                 else {
267                         /* scaling is lost but give a valid result */
268                         target[0] = dx/2.0 + (dx/2.0)*pv4[0];
269                         target[1] = dy/2.0 + (dy/2.0)*pv4[1];
270                 }
271
272         /* v3d->persmat seems to do this funky scaling */ 
273                 if(dx > dy) {
274                         y= (dx-dy)/2.0;
275                         dy = dx;
276                 }
277                 else {
278                         x= (dy-dx)/2.0;
279                         dx = dy;
280                 }
281                 target[0]= (x + target[0])/dx;
282                 target[1]= (y + target[1])/dy;
283
284                 }
285                 break;
286
287     default:
288                 target[0] = 0.0;
289                 target[1] = 1.0;
290         }
291
292         /* we know the values here and may need min_max later */
293         /* max requests independand from min; not fastest but safest */ 
294         if(min) {
295                 min[0] = MIN2(target[0], min[0]);
296                 min[1] = MIN2(target[1], min[1]);
297         }
298         if(max) {
299                 max[0] = MAX2(target[0], max[0]);
300                 max[1] = MAX2(target[1], max[1]);
301         }
302 }
303
304 static void correct_uv_aspect( EditMesh *em )
305 {
306         float aspx=1, aspy=1;
307         EditFace *efa = EM_get_actFace(em, 1);
308         MTFace *tface;
309         
310         if (efa) {
311                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
312 // XXX          image_final_aspect(tface->tpage, &aspx, &aspy);
313         }
314         
315         if (aspx != aspy) {
316                 
317                 float scale;
318                 
319                 if (aspx > aspy) {
320                         scale = aspy/aspx;
321                         for (efa= em->faces.first; efa; efa= efa->next) {
322                                 if (efa->f & SELECT) {
323                                         tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
324                                         tface->uv[0][0] = ((tface->uv[0][0]-0.5)*scale)+0.5;
325                                         tface->uv[1][0] = ((tface->uv[1][0]-0.5)*scale)+0.5;
326                                         tface->uv[2][0] = ((tface->uv[2][0]-0.5)*scale)+0.5;
327                                         if(efa->v4) {
328                                                 tface->uv[3][0] = ((tface->uv[3][0]-0.5)*scale)+0.5;
329                                         }
330                                 }
331                         }
332                 } else {
333                         scale = aspx/aspy;
334                         for (efa= em->faces.first; efa; efa= efa->next) {
335                                 if (efa->f & SELECT) {
336                                         tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
337                                         tface->uv[0][1] = ((tface->uv[0][1]-0.5)*scale)+0.5;
338                                         tface->uv[1][1] = ((tface->uv[1][1]-0.5)*scale)+0.5;
339                                         tface->uv[2][1] = ((tface->uv[2][1]-0.5)*scale)+0.5;
340                                         if(efa->v4) {
341                                                 tface->uv[3][1] = ((tface->uv[3][1]-0.5)*scale)+0.5;
342                                         }
343                                 }
344                         }
345                 }
346         }
347 }
348
349 static void default_uv(float uv[][2], float size)
350 {
351         int dy;
352         
353         if(size>1.0) size= 1.0;
354         
355         dy= 1.0-size;
356         
357         uv[0][0]= 0;
358         uv[0][1]= size+dy;
359         
360         uv[1][0]= 0;
361         uv[1][1]= dy;
362         
363         uv[2][0]= size;
364         uv[2][1]= dy;
365         
366         uv[3][0]= size;
367         uv[3][1]= size+dy;
368 }
369
370 static void calculate_uv_map(Scene *scene, ARegion *ar, View3D *v3d, EditMesh *em, unsigned short mapmode)
371 {
372         RegionView3D *rv3d= ar->regiondata;
373         MTFace *tface;
374         Object *ob;
375         EditFace *efa;
376         float dx, dy, rotatematrix[4][4], radius= 1.0, min[3], cent[3], max[3];
377         float fac= 1.0, upangledeg= 0.0, sideangledeg= 90.0;
378         int i, b, mi, n;
379         
380         if(scene->toolsettings->uvcalc_mapdir==1)  {
381                 upangledeg= 90.0;
382                 sideangledeg= 0.0;
383         } else {
384                 upangledeg= 0.0;
385                 if(scene->toolsettings->uvcalc_mapalign==1) sideangledeg= 0.0;
386                 else sideangledeg= 90.0;
387         }
388         
389         /* add uvs if there not here */
390         if (!EM_texFaceCheck(em)) {
391                 if (em && em->faces.first)
392                         EM_add_data_layer(em, &em->fdata, CD_MTFACE);
393                 
394 // XXX          if (G.sima && G.sima->image) /* this is a bit of a kludge, but assume they want the image on their mesh when UVs are added */
395 //                      image_changed(G.sima, G.sima->image);
396                 
397                 if (!EM_texFaceCheck(em))
398                         return;
399                 
400                 /* select new UV's */
401 // XX           if ((G.sima && G.sima->flag & SI_SYNC_UVSEL)==0) {
402 //                      for(efa=em->faces.first; efa; efa=efa->next) {
403 //                              MTFace *tf= (MTFace *)CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
404 //                              simaFaceSel_Set(efa, tf);
405 //                      }
406                 
407         }
408         
409         ob=OBACT;
410         
411         switch(mapmode) {
412         case B_UVAUTO_BOUNDS:
413                 min[0]= min[1]= 10000000.0;
414                 max[0]= max[1]= -10000000.0;
415
416                 cent[0] = cent[1] = cent[2] = 0.0; 
417                 uv_calc_map_matrix(rotatematrix, rv3d, ob, upangledeg, sideangledeg, 1.0f);
418                 
419                 for (efa= em->faces.first; efa; efa= efa->next) {
420                         if (efa->f & SELECT) {
421                                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
422                                 uv_calc_shift_project(ar, v3d, tface->uv[0],cent,rotatematrix,3, efa->v1->co, min,max);
423                                 uv_calc_shift_project(ar, v3d, tface->uv[1],cent,rotatematrix,3, efa->v2->co, min,max);
424                                 uv_calc_shift_project(ar, v3d, tface->uv[2],cent,rotatematrix,3, efa->v3->co,min,max);
425                                 if(efa->v4)
426                                         uv_calc_shift_project(ar, v3d, tface->uv[3],cent,rotatematrix,3, efa->v4->co,min,max);
427                         }
428                 }
429                 
430                 /* rescale UV to be in 1/1 */
431                 dx= (max[0]-min[0]);
432                 dy= (max[1]-min[1]);
433
434                 for (efa= em->faces.first; efa; efa= efa->next) {
435                         if (efa->f & SELECT) {
436                                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
437                                 if(efa->v4) b= 3; else b= 2;
438                                 for(; b>=0; b--) {
439                                         tface->uv[b][0]= ((tface->uv[b][0]-min[0])*fac)/dx;
440                                         tface->uv[b][1]= 1.0-fac+((tface->uv[b][1]-min[1])/* *fac */)/dy;
441                                 }
442                         }
443                 }
444                 break;
445
446         case B_UVAUTO_WINDOW:           
447                 cent[0] = cent[1] = cent[2] = 0.0; 
448                 Mat4CpyMat4(rotatematrix,ob->obmat);
449                 for (efa= em->faces.first; efa; efa= efa->next) {
450                         if (efa->f & SELECT) {
451                                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
452                                 uv_calc_shift_project(ar, v3d, tface->uv[0],cent,rotatematrix,4, efa->v1->co, NULL,NULL);
453                                 uv_calc_shift_project(ar, v3d, tface->uv[1],cent,rotatematrix,4, efa->v2->co, NULL,NULL);
454                                 uv_calc_shift_project(ar, v3d, tface->uv[2],cent,rotatematrix,4, efa->v3->co, NULL,NULL);
455                                 if(efa->v4)
456                                         uv_calc_shift_project(ar, v3d, tface->uv[3],cent,rotatematrix,4, efa->v4->co, NULL,NULL);
457                         }
458                 }
459                 break;
460
461         case B_UVAUTO_RESET:
462                 for (efa= em->faces.first; efa; efa= efa->next) {
463                         if (efa->f & SELECT) {
464                                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
465                                 default_uv(tface->uv, 1.0);
466                         }
467                 }
468                 break;
469
470         case B_UVAUTO_CYLINDER:
471         case B_UVAUTO_SPHERE:
472                 uv_calc_center_vector(scene, v3d, cent, ob, em);
473                         
474                 if(mapmode==B_UVAUTO_CYLINDER) radius = scene->toolsettings->uvcalc_radius;
475
476                 /* be compatible to the "old" sphere/cylinder mode */
477                 if (scene->toolsettings->uvcalc_mapdir== 2)
478                         Mat4One(rotatematrix);
479                 else 
480                         uv_calc_map_matrix(rotatematrix, rv3d, ob, upangledeg,sideangledeg,radius);
481                 for (efa= em->faces.first; efa; efa= efa->next) {
482                         if (efa->f & SELECT) {
483                                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
484                                 uv_calc_shift_project(ar, v3d, tface->uv[0],cent,rotatematrix,mapmode, efa->v1->co, NULL,NULL);
485                                 uv_calc_shift_project(ar, v3d, tface->uv[1],cent,rotatematrix,mapmode, efa->v2->co, NULL,NULL);
486                                 uv_calc_shift_project(ar, v3d, tface->uv[2],cent,rotatematrix,mapmode, efa->v3->co, NULL,NULL);
487                                 n = 3;       
488                                 if(efa->v4) {
489                                         uv_calc_shift_project(ar, v3d, tface->uv[3],cent,rotatematrix,mapmode, efa->v4->co, NULL,NULL);
490                                         n=4;
491                                 }
492
493                                 mi = 0;
494                                 for (i = 1; i < n; i++)
495                                         if (tface->uv[i][0] > tface->uv[mi][0]) mi = i;
496
497                                 for (i = 0; i < n; i++) {
498                                         if (i != mi) {
499                                                 dx = tface->uv[mi][0] - tface->uv[i][0];
500                                                 if (dx > 0.5) tface->uv[i][0] += 1.0;
501                                         } 
502                                 } 
503                         }
504                 }
505
506                 break;
507
508         case B_UVAUTO_CUBE:
509                 {
510                 /* choose x,y,z axis for projetion depending on the largest normal */
511                 /* component, but clusters all together around the center of map */
512                 float no[3];
513                 short cox, coy;
514                 float *loc= ob->obmat[3];
515                 /*MVert *mv= me->mvert;*/
516                 float cubesize = scene->toolsettings->uvcalc_cubesize;
517
518                 for (efa= em->faces.first; efa; efa= efa->next) {
519                         if (efa->f & SELECT) {
520                                 tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
521                                 CalcNormFloat(efa->v1->co, efa->v2->co, efa->v3->co, no);
522                                 
523                                 no[0]= fabs(no[0]);
524                                 no[1]= fabs(no[1]);
525                                 no[2]= fabs(no[2]);
526                                 
527                                 cox=0; coy= 1;
528                                 if(no[2]>=no[0] && no[2]>=no[1]);
529                                 else if(no[1]>=no[0] && no[1]>=no[2]) coy= 2;
530                                 else { cox= 1; coy= 2; }
531                                 
532                                 tface->uv[0][0]= 0.5+0.5*cubesize*(loc[cox] + efa->v1->co[cox]);
533                                 tface->uv[0][1]= 0.5+0.5*cubesize*(loc[coy] + efa->v1->co[coy]);
534                                 dx = floor(tface->uv[0][0]);
535                                 dy = floor(tface->uv[0][1]);
536                                 tface->uv[0][0] -= dx;
537                                 tface->uv[0][1] -= dy;
538                                 tface->uv[1][0]= 0.5+0.5*cubesize*(loc[cox] + efa->v2->co[cox]);
539                                 tface->uv[1][1]= 0.5+0.5*cubesize*(loc[coy] + efa->v2->co[coy]);
540                                 tface->uv[1][0] -= dx;
541                                 tface->uv[1][1] -= dy;
542                                 tface->uv[2][0]= 0.5+0.5*cubesize*(loc[cox] + efa->v3->co[cox]);
543                                 tface->uv[2][1]= 0.5+0.5*cubesize*(loc[coy] + efa->v3->co[coy]);
544                                 tface->uv[2][0] -= dx;
545                                 tface->uv[2][1] -= dy;
546                                 if(efa->v4) {
547                                         tface->uv[3][0]= 0.5+0.5*cubesize*(loc[cox] + efa->v4->co[cox]);
548                                         tface->uv[3][1]= 0.5+0.5*cubesize*(loc[coy] + efa->v4->co[coy]);
549                                         tface->uv[3][0] -= dx;
550                                         tface->uv[3][1] -= dy;
551                                 }
552                         }
553                 }
554                 break;
555                 }
556         default:
557                 if ((scene->toolsettings->uvcalc_flag & UVCALC_NO_ASPECT_CORRECT)==0)
558                         correct_uv_aspect(em);
559                 return;
560         } /* end switch mapmode */
561
562         /* clipping and wrapping */
563         if(0) { // XXX (make it uv layer property!) G.sima && G.sima->flag & SI_CLIP_UV) {
564                 for (efa= em->faces.first; efa; efa= efa->next) {
565                         if (!(efa->f & SELECT)) continue;
566                         tface = CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
567                 
568                         dx= dy= 0;
569                         if(efa->v4) b= 3; else b= 2;
570                         for(; b>=0; b--) {
571                                 while(tface->uv[b][0] + dx < 0.0) dx+= 0.5;
572                                 while(tface->uv[b][0] + dx > 1.0) dx-= 0.5;
573                                 while(tface->uv[b][1] + dy < 0.0) dy+= 0.5;
574                                 while(tface->uv[b][1] + dy > 1.0) dy-= 0.5;
575                         }
576         
577                         if(efa->v4) b= 3; else b= 2;
578                         for(; b>=0; b--) {
579                                 tface->uv[b][0]+= dx;
580                                 CLAMP(tface->uv[b][0], 0.0, 1.0);
581                                 
582                                 tface->uv[b][1]+= dy;
583                                 CLAMP(tface->uv[b][1], 0.0, 1.0);
584                         }
585                 }
586         }
587
588         if (    (mapmode!=B_UVAUTO_BOUNDS) &&
589                         (mapmode!=B_UVAUTO_RESET) &&
590                         (scene->toolsettings->uvcalc_flag & UVCALC_NO_ASPECT_CORRECT)==0
591                 ) {
592                 correct_uv_aspect(em);
593         }
594         
595         BIF_undo_push("UV calculation");
596
597 // XXX notifier object_uvs_changed(OBACT);
598
599 }
600
601 /* last_sel, use em->act_face otherwise get the last selected face in the editselections
602  * at the moment, last_sel is mainly useful for gaking sure the space image dosnt flicker */
603 MTFace *EM_get_active_mtface(EditMesh *em, EditFace **act_efa, MCol **mcol, int sloppy)
604 {
605         EditFace *efa = NULL;
606         
607         if(!EM_texFaceCheck(em))
608                 return NULL;
609         
610         efa = EM_get_actFace(em, sloppy);
611         
612         if (efa) {
613                 if (mcol) {
614                         if (CustomData_has_layer(&em->fdata, CD_MCOL))
615                                 *mcol = CustomData_em_get(&em->fdata, efa->data, CD_MCOL);
616                         else
617                                 *mcol = NULL;
618                 }
619                 if (act_efa) *act_efa = efa; 
620                 return CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
621         }
622         if (act_efa) *act_efa= NULL;
623         if(mcol) *mcol = NULL;
624         return NULL;
625 }
626
627 static void make_tfaces(Object *ob) 
628 {
629         Mesh *me= ob->data;
630         
631         if(!me->mtface) {
632                 me->mtface= CustomData_add_layer(&me->fdata, CD_MTFACE, CD_DEFAULT,
633                                                  NULL, me->totface);
634         }
635 }
636
637 void reveal_tface(Scene *scene)
638 {
639         Mesh *me;
640         MFace *mface;
641         int a;
642         
643         me= get_mesh(OBACT);
644         if(me==0 || me->totface==0) return;
645         
646         mface= me->mface;
647         a= me->totface;
648         while(a--) {
649                 if(mface->flag & ME_HIDE) {
650                         mface->flag |= ME_FACE_SEL;
651                         mface->flag -= ME_HIDE;
652                 }
653                 mface++;
654         }
655
656         BIF_undo_push("Reveal face");
657
658 // XXX notifier!        object_tface_flags_changed(OBACT, 0);
659 }
660
661 void hide_tface(Scene *scene)
662 {
663         Mesh *me;
664         MFace *mface;
665         int a;
666         int shift=0, alt= 0; // XXX
667         
668         me= get_mesh(OBACT);
669         if(me==0 || me->totface==0) return;
670         
671         if(alt) {
672                 reveal_tface(scene);
673                 return;
674         }
675         
676         mface= me->mface;
677         a= me->totface;
678         while(a--) {
679                 if(mface->flag & ME_HIDE);
680                 else {
681                         if(shift) {
682                                 if( (mface->flag & ME_FACE_SEL)==0) mface->flag |= ME_HIDE;
683                         }
684                         else {
685                                 if( (mface->flag & ME_FACE_SEL)) mface->flag |= ME_HIDE;
686                         }
687                 }
688                 if(mface->flag & ME_HIDE) mface->flag &= ~ME_FACE_SEL;
689                 
690                 mface++;
691         }
692
693         BIF_undo_push("Hide face");
694
695 // XXX notifier!                object_tface_flags_changed(OBACT, 0);
696 }
697
698 void select_linked_tfaces(Scene *scene, View3D *v3d, int mode)
699 {
700         Object *ob;
701         Mesh *me;
702         short mval[2];
703         unsigned int index=0;
704
705         ob = OBACT;
706         me = get_mesh(ob);
707         if(me==0 || me->totface==0) return;
708
709         if (mode==0 || mode==1) {
710                 if (!(ob->lay & v3d->lay))
711                         error("The active object is not in this layer");
712                         
713 // XXX          getmouseco_areawin(mval);
714                 if (!facesel_face_pick(v3d, me, mval, &index, 1)) return;
715         }
716
717 // XXX unwrapper.c      select_linked_tfaces_with_seams(mode, me, index);
718 }
719
720 void deselectall_tface(Scene *scene)
721 {
722         Mesh *me;
723         MFace *mface;
724         int a, sel;
725                 
726         me= get_mesh(OBACT);
727         if(me==0) return;
728         
729         mface= me->mface;
730         a= me->totface;
731         sel= 0;
732         while(a--) {
733                 if(mface->flag & ME_HIDE);
734                 else if(mface->flag & ME_FACE_SEL) sel= 1;
735                 mface++;
736         }
737         
738         mface= me->mface;
739         a= me->totface;
740         while(a--) {
741                 if(mface->flag & ME_HIDE);
742                 else {
743                         if(sel) mface->flag &= ~ME_FACE_SEL;
744                         else mface->flag |= ME_FACE_SEL;
745                 }
746                 mface++;
747         }
748
749         BIF_undo_push("(De)select all faces");
750
751 // XXX notifier!                object_tface_flags_changed(OBACT, 0);
752 }
753
754 void selectswap_tface(Scene *scene)
755 {
756         Mesh *me;
757         MFace *mface;
758         int a;
759                 
760         me= get_mesh(OBACT);
761         if(me==0) return;
762         
763         mface= me->mface;
764         a= me->totface;
765         while(a--) {
766                 if(mface->flag & ME_HIDE);
767                 else {
768                         if(mface->flag & ME_FACE_SEL) mface->flag &= ~ME_FACE_SEL;
769                         else mface->flag |= ME_FACE_SEL;
770                 }
771                 mface++;
772         }
773
774         BIF_undo_push("Select inverse face");
775
776 // XXX notifier!                object_tface_flags_changed(OBACT, 0);
777 }
778
779 int minmax_tface(Scene *scene, float *min, float *max)
780 {
781         Object *ob;
782         Mesh *me;
783         MFace *mf;
784         MTFace *tf;
785         MVert *mv;
786         int a, ok=0;
787         float vec[3], bmat[3][3];
788         
789         ob = OBACT;
790         if (ob==0) return ok;
791         me= get_mesh(ob);
792         if(me==0 || me->mtface==0) return ok;
793         
794         Mat3CpyMat4(bmat, ob->obmat);
795
796         mv= me->mvert;
797         mf= me->mface;
798         tf= me->mtface;
799         for (a=me->totface; a>0; a--, mf++, tf++) {
800                 if (mf->flag & ME_HIDE || !(mf->flag & ME_FACE_SEL))
801                         continue;
802
803                 VECCOPY(vec, (mv+mf->v1)->co);
804                 Mat3MulVecfl(bmat, vec);
805                 VecAddf(vec, vec, ob->obmat[3]);
806                 DO_MINMAX(vec, min, max);               
807
808                 VECCOPY(vec, (mv+mf->v2)->co);
809                 Mat3MulVecfl(bmat, vec);
810                 VecAddf(vec, vec, ob->obmat[3]);
811                 DO_MINMAX(vec, min, max);               
812
813                 VECCOPY(vec, (mv+mf->v3)->co);
814                 Mat3MulVecfl(bmat, vec);
815                 VecAddf(vec, vec, ob->obmat[3]);
816                 DO_MINMAX(vec, min, max);               
817
818                 if (mf->v4) {
819                         VECCOPY(vec, (mv+mf->v4)->co);
820                         Mat3MulVecfl(bmat, vec);
821                         VecAddf(vec, vec, ob->obmat[3]);
822                         DO_MINMAX(vec, min, max);
823                 }
824                 ok= 1;
825         }
826         return ok;
827 }
828
829 #define ME_SEAM_DONE 2          /* reuse this flag */
830
831 static float edgetag_cut_cost(EditMesh *em, int e1, int e2, int vert)
832 {
833         EditVert *v = EM_get_vert_for_index(vert);
834         EditEdge *eed1 = EM_get_edge_for_index(e1), *eed2 = EM_get_edge_for_index(e2);
835         EditVert *v1 = EM_get_vert_for_index( (eed1->v1->tmp.l == vert)? eed1->v2->tmp.l: eed1->v1->tmp.l );
836         EditVert *v2 = EM_get_vert_for_index( (eed2->v1->tmp.l == vert)? eed2->v2->tmp.l: eed2->v1->tmp.l );
837         float cost, d1[3], d2[3];
838
839         cost = VecLenf(v1->co, v->co);
840         cost += VecLenf(v->co, v2->co);
841
842         VecSubf(d1, v->co, v1->co);
843         VecSubf(d2, v2->co, v->co);
844
845         cost = cost + 0.5f*cost*(2.0f - fabs(d1[0]*d2[0] + d1[1]*d2[1] + d1[2]*d2[2]));
846
847         return cost;
848 }
849
850 static void edgetag_add_adjacent(EditMesh *em, Heap *heap, int mednum, int vertnum, int *nedges, int *edges, int *prevedge, float *cost)
851 {
852         int startadj, endadj = nedges[vertnum+1];
853
854         for (startadj = nedges[vertnum]; startadj < endadj; startadj++) {
855                 int adjnum = edges[startadj];
856                 EditEdge *eedadj = EM_get_edge_for_index(adjnum);
857                 float newcost;
858
859                 if (eedadj->f2 & ME_SEAM_DONE)
860                         continue;
861
862                 newcost = cost[mednum] + edgetag_cut_cost(em, mednum, adjnum, vertnum);
863
864                 if (cost[adjnum] > newcost) {
865                         cost[adjnum] = newcost;
866                         prevedge[adjnum] = mednum;
867                         BLI_heap_insert(heap, newcost, SET_INT_IN_POINTER(adjnum));
868                 }
869         }
870 }
871
872 void edgetag_context_set(Scene *scene, EditEdge *eed, int val)
873 {
874         switch (scene->toolsettings->edge_mode) {
875         case EDGE_MODE_TAG_SEAM:
876                 if (val)                {eed->seam = 255;}
877                 else                    {eed->seam = 0;}
878                 break;
879         case EDGE_MODE_TAG_SHARP:
880                 if (val)                {eed->sharp = 1;}
881                 else                    {eed->sharp = 0;}
882                 break;                          
883         case EDGE_MODE_TAG_CREASE:      
884                 if (val)                {eed->crease = 1.0f;}
885                 else                    {eed->crease = 0.0f;}
886                 break;
887         case EDGE_MODE_TAG_BEVEL:
888                 if (val)                {eed->bweight = 1.0f;}
889                 else                    {eed->bweight = 0.0f;}
890                 break;
891         }
892 }
893
894 int edgetag_context_check(Scene *scene, EditEdge *eed)
895 {
896         switch (scene->toolsettings->edge_mode) {
897         case EDGE_MODE_TAG_SEAM:
898                 return eed->seam ? 1 : 0;
899         case EDGE_MODE_TAG_SHARP:
900                 return eed->sharp ? 1 : 0;
901         case EDGE_MODE_TAG_CREASE:      
902                 return eed->crease ? 1 : 0;
903         case EDGE_MODE_TAG_BEVEL:
904                 return eed->bweight ? 1 : 0;
905         }
906         return 0;
907 }
908
909
910 int edgetag_shortest_path(Scene *scene, EditMesh *em, EditEdge *source, EditEdge *target)
911 {
912         EditEdge *eed;
913         EditVert *ev;
914         
915         Heap *heap;
916         float *cost;
917         int a, totvert=0, totedge=0, *nedges, *edges, *prevedge, mednum = -1, nedgeswap = 0;
918
919
920         /* we need the vert */
921         for (ev= em->verts.first, totvert=0; ev; ev= ev->next) {
922                 ev->tmp.l = totvert;
923                 totvert++;
924         }
925
926         for (eed= em->edges.first; eed; eed = eed->next) {
927                 eed->f2 = 0;
928                 if (eed->h) {
929                         eed->f2 |= ME_SEAM_DONE;
930                 }
931                 eed->tmp.l = totedge;
932                 totedge++;
933         }
934
935         /* alloc */
936         nedges = MEM_callocN(sizeof(*nedges)*totvert+1, "SeamPathNEdges");
937         edges = MEM_mallocN(sizeof(*edges)*totedge*2, "SeamPathEdges");
938         prevedge = MEM_mallocN(sizeof(*prevedge)*totedge, "SeamPathPrevious");
939         cost = MEM_mallocN(sizeof(*cost)*totedge, "SeamPathCost");
940
941         /* count edges, compute adjacent edges offsets and fill adjacent edges */
942         for (eed= em->edges.first; eed; eed = eed->next) {
943                 nedges[eed->v1->tmp.l+1]++;
944                 nedges[eed->v2->tmp.l+1]++;
945         }
946
947         for (a=1; a<totvert; a++) {
948                 int newswap = nedges[a+1];
949                 nedges[a+1] = nedgeswap + nedges[a];
950                 nedgeswap = newswap;
951         }
952         nedges[0] = nedges[1] = 0;
953
954         for (a=0, eed= em->edges.first; eed; a++, eed = eed->next) {
955                 edges[nedges[eed->v1->tmp.l+1]++] = a;
956                 edges[nedges[eed->v2->tmp.l+1]++] = a;
957
958                 cost[a] = 1e20f;
959                 prevedge[a] = -1;
960         }
961
962         /* regular dijkstra shortest path, but over edges instead of vertices */
963         heap = BLI_heap_new();
964         BLI_heap_insert(heap, 0.0f, SET_INT_IN_POINTER(source->tmp.l));
965         cost[source->tmp.l] = 0.0f;
966
967         EM_init_index_arrays(em, 1, 1, 0);
968
969
970         while (!BLI_heap_empty(heap)) {
971                 mednum = GET_INT_FROM_POINTER(BLI_heap_popmin(heap));
972                 eed = EM_get_edge_for_index( mednum );
973
974                 if (mednum == target->tmp.l)
975                         break;
976
977                 if (eed->f2 & ME_SEAM_DONE)
978                         continue;
979
980                 eed->f2 |= ME_SEAM_DONE;
981
982                 edgetag_add_adjacent(em, heap, mednum, eed->v1->tmp.l, nedges, edges, prevedge, cost);
983                 edgetag_add_adjacent(em, heap, mednum, eed->v2->tmp.l, nedges, edges, prevedge, cost);
984         }
985         
986         
987         MEM_freeN(nedges);
988         MEM_freeN(edges);
989         MEM_freeN(cost);
990         BLI_heap_free(heap, NULL);
991
992         for (eed= em->edges.first; eed; eed = eed->next) {
993                 eed->f2 &= ~ME_SEAM_DONE;
994         }
995
996         if (mednum != target->tmp.l) {
997                 MEM_freeN(prevedge);
998                 EM_free_index_arrays();
999                 return 0;
1000         }
1001
1002         /* follow path back to source and mark as seam */
1003         if (mednum == target->tmp.l) {
1004                 short allseams = 1;
1005
1006                 mednum = target->tmp.l;
1007                 do {
1008                         eed = EM_get_edge_for_index( mednum );
1009                         if (!edgetag_context_check(scene, eed)) {
1010                                 allseams = 0;
1011                                 break;
1012                         }
1013                         mednum = prevedge[mednum];
1014                 } while (mednum != source->tmp.l);
1015
1016                 mednum = target->tmp.l;
1017                 do {
1018                         eed = EM_get_edge_for_index( mednum );
1019                         if (allseams)
1020                                 edgetag_context_set(scene, eed, 0);
1021                         else
1022                                 edgetag_context_set(scene, eed, 1);
1023                         mednum = prevedge[mednum];
1024                 } while (mednum != -1);
1025         }
1026
1027         MEM_freeN(prevedge);
1028         EM_free_index_arrays();
1029         return 1;
1030 }
1031
1032 static void seam_edgehash_insert_face(EdgeHash *ehash, MFace *mf)
1033 {
1034         BLI_edgehash_insert(ehash, mf->v1, mf->v2, NULL);
1035         BLI_edgehash_insert(ehash, mf->v2, mf->v3, NULL);
1036         if (mf->v4) {
1037                 BLI_edgehash_insert(ehash, mf->v3, mf->v4, NULL);
1038                 BLI_edgehash_insert(ehash, mf->v4, mf->v1, NULL);
1039         }
1040         else
1041                 BLI_edgehash_insert(ehash, mf->v3, mf->v1, NULL);
1042 }
1043
1044 void seam_mark_clear_tface(Scene *scene, short mode)
1045 {
1046         Mesh *me;
1047         MFace *mf;
1048         MEdge *med;
1049         int a;
1050         
1051         me= get_mesh(OBACT);
1052         if(me==0 ||  me->totface==0) return;
1053
1054         if (mode == 0)
1055                 mode = pupmenu("Seams%t|Mark Border Seam %x1|Clear Seam %x2");
1056
1057         if (mode != 1 && mode != 2)
1058                 return;
1059
1060         if (mode == 2) {
1061                 EdgeHash *ehash = BLI_edgehash_new();
1062
1063                 for (a=0, mf=me->mface; a<me->totface; a++, mf++)
1064                         if (!(mf->flag & ME_HIDE) && (mf->flag & ME_FACE_SEL))
1065                                 seam_edgehash_insert_face(ehash, mf);
1066
1067                 for (a=0, med=me->medge; a<me->totedge; a++, med++)
1068                         if (BLI_edgehash_haskey(ehash, med->v1, med->v2))
1069                                 med->flag &= ~ME_SEAM;
1070
1071                 BLI_edgehash_free(ehash, NULL);
1072         }
1073         else {
1074                 /* mark edges that are on both selected and deselected faces */
1075                 EdgeHash *ehash1 = BLI_edgehash_new();
1076                 EdgeHash *ehash2 = BLI_edgehash_new();
1077
1078                 for (a=0, mf=me->mface; a<me->totface; a++, mf++) {
1079                         if ((mf->flag & ME_HIDE) || !(mf->flag & ME_FACE_SEL))
1080                                 seam_edgehash_insert_face(ehash1, mf);
1081                         else
1082                                 seam_edgehash_insert_face(ehash2, mf);
1083                 }
1084
1085                 for (a=0, med=me->medge; a<me->totedge; a++, med++)
1086                         if (BLI_edgehash_haskey(ehash1, med->v1, med->v2) &&
1087                             BLI_edgehash_haskey(ehash2, med->v1, med->v2))
1088                                 med->flag |= ME_SEAM;
1089
1090                 BLI_edgehash_free(ehash1, NULL);
1091                 BLI_edgehash_free(ehash2, NULL);
1092         }
1093
1094 // XXX  if (G.rt == 8)
1095 //              unwrap_lscm(1);
1096
1097         G.f |= G_DRAWSEAMS;
1098         BIF_undo_push("Mark Seam");
1099
1100 // XXX notifier!                object_tface_flags_changed(OBACT, 1);
1101 }
1102
1103 void face_select(Scene *scene, View3D *v3d)
1104 {
1105         Object *ob;
1106         Mesh *me;
1107         MFace *mface, *msel;
1108         short mval[2];
1109         unsigned int a, index;
1110         int shift= 0; // XXX
1111         
1112         /* Get the face under the cursor */
1113         ob = OBACT;
1114         if (!(ob->lay & v3d->lay)) {
1115                 error("The active object is not in this layer");
1116         }
1117         me = get_mesh(ob);
1118 // XXX  getmouseco_areawin(mval);
1119
1120         if (!facesel_face_pick(v3d, me, mval, &index, 1)) return;
1121         
1122         msel= (((MFace*)me->mface)+index);
1123         if (msel->flag & ME_HIDE) return;
1124         
1125         /* clear flags */
1126         mface = me->mface;
1127         a = me->totface;
1128         if ((shift)==0) {
1129                 while (a--) {
1130                         mface->flag &= ~ME_FACE_SEL;
1131                         mface++;
1132                 }
1133         }
1134         
1135         me->act_face = (int)index;
1136
1137         if (shift) {
1138                 if (msel->flag & ME_FACE_SEL)
1139                         msel->flag &= ~ME_FACE_SEL;
1140                 else
1141                         msel->flag |= ME_FACE_SEL;
1142         }
1143         else msel->flag |= ME_FACE_SEL;
1144         
1145         /* image window redraw */
1146         
1147         BIF_undo_push("Select UV face");
1148
1149 // XXX notifier!                object_tface_flags_changed(OBACT, 1);
1150 }
1151
1152 void face_borderselect(Scene *scene, ARegion *ar)
1153 {
1154         Mesh *me;
1155         MFace *mface;
1156         rcti rect;
1157         struct ImBuf *ibuf;
1158         unsigned int *rt;
1159         int a, sx, sy, index, val;
1160         char *selar;
1161         
1162         me= get_mesh(OBACT);
1163         if(me==0) return;
1164         if(me->totface==0) return;
1165         
1166 // XXX  val= get_border(&rect, 3);
1167         
1168         /* why readbuffer here? shouldn't be necessary (maybe a flush or so) */
1169         glReadBuffer(GL_BACK);
1170 #ifdef __APPLE__
1171         glReadBuffer(GL_AUX0); /* apple only */
1172 #endif
1173         
1174         if(val) {
1175                 selar= MEM_callocN(me->totface+1, "selar");
1176                 
1177                 sx= (rect.xmax-rect.xmin+1);
1178                 sy= (rect.ymax-rect.ymin+1);
1179                 if(sx*sy<=0) return;
1180
1181                 ibuf = IMB_allocImBuf(sx,sy,32,IB_rect,0);
1182                 rt = ibuf->rect;
1183                 glReadPixels(rect.xmin+ar->winrct.xmin,  rect.ymin+ar->winrct.ymin, sx, sy, GL_RGBA, GL_UNSIGNED_BYTE,  ibuf->rect);
1184                 if(ENDIAN_ORDER==B_ENDIAN) IMB_convert_rgba_to_abgr(ibuf);
1185
1186                 a= sx*sy;
1187                 while(a--) {
1188                         if(*rt) {
1189                                 index= WM_framebuffer_to_index(*rt);
1190                                 if(index<=me->totface) selar[index]= 1;
1191                         }
1192                         rt++;
1193                 }
1194                 
1195                 mface= me->mface;
1196                 for(a=1; a<=me->totface; a++, mface++) {
1197                         if(selar[a]) {
1198                                 if(mface->flag & ME_HIDE);
1199                                 else {
1200                                         if(val==LEFTMOUSE) mface->flag |= ME_FACE_SEL;
1201                                         else mface->flag &= ~ME_FACE_SEL;
1202                                 }
1203                         }
1204                 }
1205                 
1206                 IMB_freeImBuf(ibuf);
1207                 MEM_freeN(selar);
1208
1209                 BIF_undo_push("Border Select UV face");
1210
1211 // XXX notifier!                        object_tface_flags_changed(OBACT, 0);
1212         }
1213 #ifdef __APPLE__        
1214         glReadBuffer(GL_BACK);
1215 #endif
1216 }
1217
1218 void uv_autocalc_tface(Scene *scene, ARegion *ar, View3D *v3d, EditMesh *em)
1219 {
1220         short mode;
1221 #ifndef DISABLE_PYTHON
1222 //      short i=0, has_pymenu=0; /* pymenu must be bigger then UV_*_MAPPING */
1223 // XXX  BPyMenu *pym;
1224 //      char menu_number[3];
1225 #endif
1226         
1227         /* uvmenu, will add python items */
1228         char uvmenu[4096]=MENUTITLE("UV Calculation")
1229                                         MENUSTRING("Unwrap",                            UV_UNWRAP_MAPPING) "|%l|"
1230                                         
1231                                         MENUSTRING("Cube Projection",                   UV_CUBE_MAPPING) "|"
1232                                         MENUSTRING("Cylinder from View",                UV_CYL_MAPPING) "|"
1233                                         MENUSTRING("Sphere from View",                  UV_SPHERE_MAPPING) "|%l|"
1234
1235                                         MENUSTRING("Project From View",                 UV_WINDOW_MAPPING) "|"
1236                                         MENUSTRING("Project from View (Bounds)",UV_BOUNDS_MAPPING) "|%l|"
1237                                         
1238                                         MENUSTRING("Reset",                                             UV_RESET_MAPPING);
1239 #ifndef DISABLE_PYTHON
1240 #if 0
1241         XXX
1242         /* note that we account for the 10 previous entries with i+10: */
1243         for (pym = BPyMenuTable[PYMENU_UVCALCULATION]; pym; pym = pym->next, i++) {
1244                 
1245                 if (!has_pymenu) {
1246                         strcat(uvmenu, "|%l");
1247                         has_pymenu = 1;
1248                 }
1249                 
1250                 strcat(uvmenu, "|");
1251                 strcat(uvmenu, pym->name);
1252                 strcat(uvmenu, " %x");
1253                 sprintf(menu_number, "%d", i+10);
1254                 strcat(uvmenu, menu_number);
1255         }
1256 #endif
1257 #endif
1258         
1259         mode= pupmenu(uvmenu);
1260 #ifndef DISABLE_PYTHON
1261 //      if (mode >= 10) {
1262 //              BPY_menu_do_python(PYMENU_UVCALCULATION, mode - 10);
1263 //              return;
1264 //      }
1265 #endif  
1266         switch(mode) {
1267         case UV_CUBE_MAPPING:
1268                 calculate_uv_map(scene, ar, v3d, em, B_UVAUTO_CUBE); break;
1269         case UV_CYL_MAPPING:
1270                 calculate_uv_map(scene, ar, v3d, em, B_UVAUTO_CYLINDER); break;
1271         case UV_SPHERE_MAPPING:
1272                 calculate_uv_map(scene, ar, v3d, em, B_UVAUTO_SPHERE); break;
1273         case UV_BOUNDS_MAPPING:
1274                 calculate_uv_map(scene, ar, v3d, em, B_UVAUTO_BOUNDS); break;
1275         case UV_RESET_MAPPING:
1276                 calculate_uv_map(scene, ar, v3d, em, B_UVAUTO_RESET); break;
1277         case UV_WINDOW_MAPPING:
1278                 calculate_uv_map(scene, ar, v3d, em, B_UVAUTO_WINDOW); break;
1279         case UV_UNWRAP_MAPPING:
1280 // XXX          unwrap_lscm(0); 
1281                 break;
1282         }
1283 }
1284
1285 /* Texture Paint */
1286
1287 void set_texturepaint(Scene *scene) /* toggle */
1288 {
1289         Object *ob = OBACT;
1290         Mesh *me = 0;
1291         
1292         if(ob==NULL) return;
1293         
1294         if (object_data_is_libdata(ob)) {
1295 // XXX          error_libdata();
1296                 return;
1297         }
1298
1299         me= get_mesh(ob);
1300         
1301         if(me)
1302                 DAG_object_flush_update(scene, ob, OB_RECALC_DATA);
1303
1304         if(G.f & G_TEXTUREPAINT) {
1305                 G.f &= ~G_TEXTUREPAINT;
1306                 GPU_paint_set_mipmap(1);
1307         }
1308         else if (me) {
1309                 G.f |= G_TEXTUREPAINT;
1310
1311                 if(me->mtface==NULL)
1312                         make_tfaces(ob);
1313
1314                 brush_check_exists(&scene->toolsettings->imapaint.brush);
1315                 GPU_paint_set_mipmap(0);
1316         }
1317
1318 }
1319
1320 static void texpaint_project(Object *ob, float *model, float *proj, float *co, float *pco)
1321 {
1322         VECCOPY(pco, co);
1323         pco[3]= 1.0f;
1324
1325         Mat4MulVecfl(ob->obmat, pco);
1326         Mat4MulVecfl((float(*)[4])model, pco);
1327         Mat4MulVec4fl((float(*)[4])proj, pco);
1328 }
1329
1330 static void texpaint_tri_weights(Object *ob, float *v1, float *v2, float *v3, float *co, float *w)
1331 {
1332         float pv1[4], pv2[4], pv3[4], h[3], divw;
1333         float model[16], proj[16], wmat[3][3], invwmat[3][3];
1334         GLint view[4];
1335
1336         /* compute barycentric coordinates */
1337
1338         /* get the needed opengl matrices */
1339         glGetIntegerv(GL_VIEWPORT, view);
1340         glGetFloatv(GL_MODELVIEW_MATRIX, model);
1341         glGetFloatv(GL_PROJECTION_MATRIX, proj);
1342         view[0] = view[1] = 0;
1343
1344         /* project the verts */
1345         texpaint_project(ob, model, proj, v1, pv1);
1346         texpaint_project(ob, model, proj, v2, pv2);
1347         texpaint_project(ob, model, proj, v3, pv3);
1348
1349         /* do inverse view mapping, see gluProject man page */
1350         h[0]= (co[0] - view[0])*2.0f/view[2] - 1;
1351         h[1]= (co[1] - view[1])*2.0f/view[3] - 1;
1352         h[2]= 1.0f;
1353
1354         /* solve for (w1,w2,w3)/perspdiv in:
1355            h*perspdiv = Project*Model*(w1*v1 + w2*v2 + w3*v3) */
1356
1357         wmat[0][0]= pv1[0];  wmat[1][0]= pv2[0];  wmat[2][0]= pv3[0];
1358         wmat[0][1]= pv1[1];  wmat[1][1]= pv2[1];  wmat[2][1]= pv3[1];
1359         wmat[0][2]= pv1[3];  wmat[1][2]= pv2[3];  wmat[2][2]= pv3[3];
1360
1361         Mat3Inv(invwmat, wmat);
1362         Mat3MulVecfl(invwmat, h);
1363
1364         VECCOPY(w, h);
1365
1366         /* w is still divided by perspdiv, make it sum to one */
1367         divw= w[0] + w[1] + w[2];
1368         if(divw != 0.0f)
1369                 VecMulf(w, 1.0f/divw);
1370 }
1371
1372 /* compute uv coordinates of mouse in face */
1373 void texpaint_pick_uv(Scene *scene, Object *ob, Mesh *mesh, unsigned int faceindex, short *xy, float *uv)
1374 {
1375         DerivedMesh *dm = mesh_get_derived_final(scene, ob, CD_MASK_BAREMESH);
1376         int *index = dm->getFaceDataArray(dm, CD_ORIGINDEX);
1377         MTFace *tface = dm->getFaceDataArray(dm, CD_MTFACE), *tf;
1378         int numfaces = dm->getNumFaces(dm), a;
1379         float p[2], w[3], absw, minabsw;
1380         MFace mf;
1381         MVert mv[4];
1382
1383         minabsw = 1e10;
1384         uv[0] = uv[1] = 0.0;
1385
1386 // XXX  persp(PERSP_VIEW);
1387
1388         /* test all faces in the derivedmesh with the original index of the picked face */
1389         for (a = 0; a < numfaces; a++) {
1390                 if (index[a] == faceindex) {
1391                         dm->getFace(dm, a, &mf);
1392
1393                         dm->getVert(dm, mf.v1, &mv[0]);
1394                         dm->getVert(dm, mf.v2, &mv[1]);
1395                         dm->getVert(dm, mf.v3, &mv[2]);
1396                         if (mf.v4)
1397                                 dm->getVert(dm, mf.v4, &mv[3]);
1398
1399                         tf= &tface[a];
1400
1401                         p[0]= xy[0];
1402                         p[1]= xy[1];
1403
1404                         if (mf.v4) {
1405                                 /* the triangle with the largest absolute values is the one
1406                                    with the most negative weights */
1407                                 texpaint_tri_weights(ob, mv[0].co, mv[1].co, mv[3].co, p, w);
1408                                 absw= fabs(w[0]) + fabs(w[1]) + fabs(w[2]);
1409                                 if(absw < minabsw) {
1410                                         uv[0]= tf->uv[0][0]*w[0] + tf->uv[1][0]*w[1] + tf->uv[3][0]*w[2];
1411                                         uv[1]= tf->uv[0][1]*w[0] + tf->uv[1][1]*w[1] + tf->uv[3][1]*w[2];
1412                                         minabsw = absw;
1413                                 }
1414
1415                                 texpaint_tri_weights(ob, mv[1].co, mv[2].co, mv[3].co, p, w);
1416                                 absw= fabs(w[0]) + fabs(w[1]) + fabs(w[2]);
1417                                 if (absw < minabsw) {
1418                                         uv[0]= tf->uv[1][0]*w[0] + tf->uv[2][0]*w[1] + tf->uv[3][0]*w[2];
1419                                         uv[1]= tf->uv[1][1]*w[0] + tf->uv[2][1]*w[1] + tf->uv[3][1]*w[2];
1420                                         minabsw = absw;
1421                                 }
1422                         }
1423                         else {
1424                                 texpaint_tri_weights(ob, mv[0].co, mv[1].co, mv[2].co, p, w);
1425                                 absw= fabs(w[0]) + fabs(w[1]) + fabs(w[2]);
1426                                 if (absw < minabsw) {
1427                                         uv[0]= tf->uv[0][0]*w[0] + tf->uv[1][0]*w[1] + tf->uv[2][0]*w[2];
1428                                         uv[1]= tf->uv[0][1]*w[0] + tf->uv[1][1]*w[1] + tf->uv[2][1]*w[2];
1429                                         minabsw = absw;
1430                                 }
1431                         }
1432                 }
1433         }
1434
1435         dm->release(dm);
1436 }