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