Bring back "Sort Faces" command.
[blender.git] / source / blender / editors / mesh / meshtools.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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  *
20  * The Original Code is Copyright (C) 2004 by Blender Foundation
21  * All rights reserved.
22  *
23  * The Original Code is: all of this file.
24  *
25  * Contributor(s): none yet.
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  */
29
30 /*
31         meshtools.c: no editmode (violated already :), tools operating on meshes
32 */
33
34 #include <stddef.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <math.h>
38 #include <float.h>
39
40 #include "MEM_guardedalloc.h"
41
42 #include "DNA_key_types.h"
43 #include "DNA_material_types.h"
44 #include "DNA_meshdata_types.h"
45 #include "DNA_object_types.h"
46 #include "DNA_scene_types.h"
47 #include "DNA_view3d_types.h"
48
49 #include "BLI_math.h"
50 #include "BLI_blenlib.h"
51 #include "BLI_editVert.h"
52 #include "BLI_ghash.h"
53 #include "BLI_rand.h" /* for randome face sorting */
54 #include "BLI_threads.h"
55
56
57 #include "BKE_blender.h"
58 #include "BKE_context.h"
59 #include "BKE_depsgraph.h"
60 #include "BKE_DerivedMesh.h"
61 #include "BKE_customdata.h"
62 #include "BKE_global.h"
63 #include "BKE_image.h"
64 #include "BKE_key.h"
65 #include "BKE_library.h"
66 #include "BKE_main.h"
67 #include "BKE_mesh.h"
68 #include "BKE_material.h"
69 #include "BKE_object.h"
70 #include "BKE_utildefines.h"
71 #include "BKE_report.h"
72
73 #include "BLO_sys_types.h" // for intptr_t support
74
75 #include "ED_mesh.h"
76 #include "ED_object.h"
77 #include "ED_view3d.h"
78
79 #include "WM_api.h"
80 #include "WM_types.h"
81
82 /* own include */
83 #include "mesh_intern.h"
84
85 /* XXX */
86 static int pupmenu(const char *dummy) {return 0;}
87 /* XXX */
88
89
90 /* * ********************** no editmode!!! *********** */
91
92 /*********************** JOIN ***************************/
93
94 /* join selected meshes into the active mesh, context sensitive
95 return 0 if no join is made (error) and 1 of the join is done */
96
97 int join_mesh_exec(bContext *C, wmOperator *op)
98 {
99         Scene *scene= CTX_data_scene(C);
100         Object *ob= CTX_data_active_object(C);
101         Material **matar, *ma;
102         Mesh *me;
103         MVert *mvert, *mv, *mvertmain;
104         MEdge *medge = NULL, *medgemain;
105         MFace *mface = NULL, *mfacemain;
106         Key *key, *nkey=NULL;
107         KeyBlock *kb, *okb, *kbn;
108         float imat[4][4], cmat[4][4], *fp1, *fp2, curpos;
109         int a, b, totcol, totmat=0, totedge=0, totvert=0, totface=0, ok=0;
110         int vertofs, *matmap=NULL;
111         int     i, j, index, haskey=0, edgeofs, faceofs;
112         bDeformGroup *dg, *odg;
113         MDeformVert *dvert;
114         CustomData vdata, edata, fdata;
115
116         if(scene->obedit)
117                 return OPERATOR_CANCELLED;
118         
119         /* ob is the object we are adding geometry to */
120         if(!ob || ob->type!=OB_MESH)
121                 return OPERATOR_CANCELLED;
122         
123         /* count & check */
124         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
125                 if(base->object->type==OB_MESH) {
126                         me= base->object->data;
127                         
128                         totvert+= me->totvert;
129                         totedge+= me->totedge;
130                         totface+= me->totface;
131                         totmat+= base->object->totcol;
132                         
133                         if(base->object == ob)
134                                 ok= 1;
135                         
136                         /* check for shapekeys */
137                         if(me->key)
138                                 haskey++;
139                 }
140         }
141         CTX_DATA_END;
142         
143         /* that way the active object is always selected */ 
144         if(ok==0)
145                 return OPERATOR_CANCELLED;
146         
147         /* only join meshes if there are verts to join, there aren't too many, and we only had one mesh selected */
148         me= (Mesh *)ob->data;
149         key= me->key;
150         if(totvert==0 || totvert>MESH_MAX_VERTS || totvert==me->totvert) 
151                 return OPERATOR_CANCELLED;
152         
153         /* new material indices and material array */
154         matar= MEM_callocN(sizeof(void*)*totmat, "join_mesh matar");
155         if (totmat) matmap= MEM_callocN(sizeof(int)*totmat, "join_mesh matmap");
156         totcol= ob->totcol;
157         
158         /* obact materials in new main array, is nicer start! */
159         for(a=0; a<ob->totcol; a++) {
160                 matar[a]= give_current_material(ob, a+1);
161                 id_us_plus((ID *)matar[a]);
162                 /* increase id->us : will be lowered later */
163         }
164         
165         /* - if destination mesh had shapekeys, move them somewhere safe, and set up placeholders
166          *      with arrays that are large enough to hold shapekey data for all meshes
167          * -    if destination mesh didn't have shapekeys, but we encountered some in the meshes we're 
168          *      joining, set up a new keyblock and assign to the mesh
169          */
170         if(key) {
171                 /* make a duplicate copy that will only be used here... (must remember to free it!) */
172                 nkey= copy_key(key);
173                 
174                 /* for all keys in old block, clear data-arrays */
175                 for(kb= key->block.first; kb; kb= kb->next) {
176                         if(kb->data) MEM_freeN(kb->data);
177                         kb->data= MEM_callocN(sizeof(float)*3*totvert, "join_shapekey");
178                         kb->totelem= totvert;
179                         kb->weights= NULL;
180                 }
181         }
182         else if(haskey) {
183                 /* add a new key-block and add to the mesh */
184                 key= me->key= add_key((ID *)me);
185                 key->type = KEY_RELATIVE;
186         }
187         
188         /* first pass over objects - copying materials and vertexgroups across */
189         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
190                 /* only act if a mesh, and not the one we're joining to */
191                 if((ob!=base->object) && (base->object->type==OB_MESH)) {
192                         me= base->object->data;
193                         
194                         /* Join this object's vertex groups to the base one's */
195                         for(dg=base->object->defbase.first; dg; dg=dg->next) {
196                                 /* See if this group exists in the object (if it doesn't, add it to the end) */
197                                 for(odg=ob->defbase.first; odg; odg=odg->next) {
198                                         if(!strcmp(odg->name, dg->name)) {
199                                                 break;
200                                         }
201                                 }
202                                 if(!odg) {
203                                         odg = MEM_callocN(sizeof(bDeformGroup), "join deformGroup");
204                                         memcpy(odg, dg, sizeof(bDeformGroup));
205                                         BLI_addtail(&ob->defbase, odg);
206                                 }
207                         }
208                         if(ob->defbase.first && ob->actdef==0)
209                                 ob->actdef=1;
210                         
211                         
212                         if(me->totvert) {
213                                 /* Add this object's materials to the base one's if they don't exist already (but only if limits not exceeded yet) */
214                                 if(totcol < MAXMAT-1) {
215                                         for(a=1; a<=base->object->totcol; a++) {
216                                                 ma= give_current_material(base->object, a);
217
218                                                 for(b=0; b<totcol; b++) {
219                                                         if(ma == matar[b]) break;
220                                                 }
221                                                 if(b==totcol) {
222                                                         matar[b]= ma;
223                                                         if(ma)
224                                                                 ma->id.us++;
225                                                         totcol++;
226                                                 }
227                                                 if(totcol>=MAXMAT-1) 
228                                                         break;
229                                         }
230                                 }
231                                 
232                                 /* if this mesh has shapekeys, check if destination mesh already has matching entries too */
233                                 if(me->key && key) {
234                                         for(kb= me->key->block.first; kb; kb= kb->next) {
235                                                 /* if key doesn't exist in destination mesh, add it */
236                                                 if(key_get_named_keyblock(key, kb->name) == NULL) {
237                                                         /* copy this existing one over to the new shapekey block */
238                                                         kbn= MEM_dupallocN(kb);
239                                                         kbn->prev= kbn->next= NULL;
240                                                         
241                                                         /* adjust adrcode and other settings to fit (allocate a new data-array) */
242                                                         kbn->data= MEM_callocN(sizeof(float)*3*totvert, "joined_shapekey");
243                                                         kbn->totelem= totvert;
244                                                         kbn->weights= NULL;
245                                                         
246                                                         okb= key->block.last;
247                                                         curpos= (okb) ? okb->pos : -0.1f;
248                                                         if(key->type == KEY_RELATIVE)
249                                                                 kbn->pos= curpos + 0.1f;
250                                                         else
251                                                                 kbn->pos= curpos;
252                                                         
253                                                         BLI_addtail(&key->block, kbn);
254                                                         kbn->adrcode= key->totkey;
255                                                         key->totkey++;
256                                                         if(key->totkey==1) key->refkey= kbn;
257                                                         
258                                                         // XXX 2.5 Animato
259 #if 0
260                                                         /* also, copy corresponding ipo-curve to ipo-block if applicable */
261                                                         if(me->key->ipo && key->ipo) {
262                                                                 // FIXME... this is a luxury item!
263                                                                 puts("FIXME: ignoring IPO's when joining shapekeys on Meshes for now...");
264                                                         }
265 #endif
266                                                 }
267                                         }
268                                 }
269                         }
270                 }
271         }
272         CTX_DATA_END;
273         
274         /* setup new data for destination mesh */
275         memset(&vdata, 0, sizeof(vdata));
276         memset(&edata, 0, sizeof(edata));
277         memset(&fdata, 0, sizeof(fdata));
278         
279         mvert= CustomData_add_layer(&vdata, CD_MVERT, CD_CALLOC, NULL, totvert);
280         medge= CustomData_add_layer(&edata, CD_MEDGE, CD_CALLOC, NULL, totedge);
281         mface= CustomData_add_layer(&fdata, CD_MFACE, CD_CALLOC, NULL, totface);
282         
283         mvertmain= mvert;
284         medgemain= medge;
285         mfacemain= mface;
286         
287         vertofs= 0;
288         edgeofs= 0;
289         faceofs= 0;
290         
291         /* inverse transform for all selected meshes in this object */
292         invert_m4_m4(imat, ob->obmat);
293         
294         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
295                 /* only join if this is a mesh */
296                 if(base->object->type==OB_MESH) {
297                         me= base->object->data;
298                         
299                         if(me->totvert) {
300                                 /* standard data */
301                                 CustomData_merge(&me->vdata, &vdata, CD_MASK_MESH, CD_DEFAULT, totvert);
302                                 CustomData_copy_data(&me->vdata, &vdata, 0, vertofs, me->totvert);
303                                 
304                                 /* vertex groups */
305                                 dvert= CustomData_get(&vdata, vertofs, CD_MDEFORMVERT);
306                                 
307                                 /* NB: vertex groups here are new version */
308                                 if(dvert) {
309                                         for(i=0; i<me->totvert; i++) {
310                                                 for(j=0; j<dvert[i].totweight; j++) {
311                                                         /*      Find the old vertex group */
312                                                         odg = BLI_findlink(&base->object->defbase, dvert[i].dw[j].def_nr);
313                                                         if(odg) {
314                                                                 /*      Search for a match in the new object, and set new index */
315                                                                 for(dg=ob->defbase.first, index=0; dg; dg=dg->next, index++) {
316                                                                         if(!strcmp(dg->name, odg->name)) {
317                                                                                 dvert[i].dw[j].def_nr = index;
318                                                                                 break;
319                                                                         }
320                                                                 }
321                                                         }
322                                                 }
323                                         }
324                                 }
325                                 
326                                 /* if this is the object we're merging into, no need to do anything */
327                                 if(base->object != ob) {
328                                         /* watch this: switch matmul order really goes wrong */
329                                         mul_m4_m4m4(cmat, base->object->obmat, imat);
330                                         
331                                         /* transform vertex coordinates into new space */
332                                         for(a=0, mv=mvert; a < me->totvert; a++, mv++) {
333                                                 mul_m4_v3(cmat, mv->co);
334                                         }
335                                         
336                                         /* for each shapekey in destination mesh:
337                                          *      - if there's a matching one, copy it across (will need to transform vertices into new space...)
338                                          *      - otherwise, just copy own coordinates of mesh (no need to transform vertex coordinates into new space)
339                                          */
340                                         if(key) {
341                                                 /* if this mesh has any shapekeys, check first, otherwise just copy coordinates */
342                                                 for(kb= key->block.first; kb; kb= kb->next) {
343                                                         /* get pointer to where to write data for this mesh in shapekey's data array */
344                                                         fp1= ((float *)kb->data) + (vertofs*3); 
345                                                         
346                                                         /* check if this mesh has such a shapekey */
347                                                         okb= key_get_named_keyblock(me->key, kb->name);
348                                                         if(okb) {
349                                                                 /* copy this mesh's shapekey to the destination shapekey (need to transform first) */
350                                                                 fp2= ((float *)(okb->data));
351                                                                 for(a=0; a < me->totvert; a++, fp1+=3, fp2+=3) {
352                                                                         VECCOPY(fp1, fp2);
353                                                                         mul_m4_v3(cmat, fp1);
354                                                                 }
355                                                         }
356                                                         else {
357                                                                 /* copy this mesh's vertex coordinates to the destination shapekey */
358                                                                 mv= mvert;
359                                                                 for(a=0; a < me->totvert; a++, fp1+=3, mv++) {
360                                                                         VECCOPY(fp1, mv->co);
361                                                                 }
362                                                         }
363                                                 }
364                                         }
365                                 }
366                                 else {
367                                         /* for each shapekey in destination mesh:
368                                          *      - if it was an 'original', copy the appropriate data from nkey
369                                          *      - otherwise, copy across plain coordinates (no need to transform coordinates)
370                                          */
371                                         if(key) {
372                                                 for(kb= key->block.first; kb; kb= kb->next) {
373                                                         /* get pointer to where to write data for this mesh in shapekey's data array */
374                                                         fp1= ((float *)kb->data) + (vertofs*3); 
375                                                         
376                                                         /* check if this was one of the original shapekeys */
377                                                         okb= key_get_named_keyblock(nkey, kb->name);
378                                                         if(okb) {
379                                                                 /* copy this mesh's shapekey to the destination shapekey */
380                                                                 fp2= ((float *)(okb->data));
381                                                                 for(a=0; a < me->totvert; a++, fp1+=3, fp2+=3) {
382                                                                         VECCOPY(fp1, fp2);
383                                                                 }
384                                                         }
385                                                         else {
386                                                                 /* copy base-coordinates to the destination shapekey */
387                                                                 mv= mvert;
388                                                                 for(a=0; a < me->totvert; a++, fp1+=3, mv++) {
389                                                                         VECCOPY(fp1, mv->co);
390                                                                 }
391                                                         }
392                                                 }
393                                         }
394                                 }
395                                 
396                                 /* advance mvert pointer to end of base mesh's data */
397                                 mvert+= me->totvert;
398                         }
399                         
400                         if(me->totface) {
401                                 /* make mapping for materials */
402                                 for(a=1; a<=base->object->totcol; a++) {
403                                         ma= give_current_material(base->object, a);
404
405                                         for(b=0; b<totcol; b++) {
406                                                 if(ma == matar[b]) {
407                                                         matmap[a-1]= b;
408                                                         break;
409                                                 }
410                                         }
411                                 }
412                                 
413                                 CustomData_merge(&me->fdata, &fdata, CD_MASK_MESH, CD_DEFAULT, totface);
414                                 CustomData_copy_data(&me->fdata, &fdata, 0, faceofs, me->totface);
415                                 
416                                 for(a=0; a<me->totface; a++, mface++) {
417                                         mface->v1+= vertofs;
418                                         mface->v2+= vertofs;
419                                         mface->v3+= vertofs;
420                                         if(mface->v4) mface->v4+= vertofs;
421                                         
422                                         if (matmap)
423                                                 mface->mat_nr= matmap[(int)mface->mat_nr];
424                                         else 
425                                                 mface->mat_nr= 0;
426                                 }
427                                 
428                                 faceofs += me->totface;
429                         }
430                         
431                         if(me->totedge) {
432                                 CustomData_merge(&me->edata, &edata, CD_MASK_MESH, CD_DEFAULT, totedge);
433                                 CustomData_copy_data(&me->edata, &edata, 0, edgeofs, me->totedge);
434                                 
435                                 for(a=0; a<me->totedge; a++, medge++) {
436                                         medge->v1+= vertofs;
437                                         medge->v2+= vertofs;
438                                 }
439                                 
440                                 edgeofs += me->totedge;
441                         }
442                         
443                         /* vertofs is used to help newly added verts be reattached to their edge/face 
444                          * (cannot be set earlier, or else reattaching goes wrong)
445                          */
446                         vertofs += me->totvert;
447                         
448                         /* free base, now that data is merged */
449                         if(base->object != ob)
450                                 ED_base_object_free_and_unlink(scene, base);
451                 }
452         }
453         CTX_DATA_END;
454         
455         /* return to mesh we're merging to */
456         me= ob->data;
457         
458         CustomData_free(&me->vdata, me->totvert);
459         CustomData_free(&me->edata, me->totedge);
460         CustomData_free(&me->fdata, me->totface);
461
462         me->totvert= totvert;
463         me->totedge= totedge;
464         me->totface= totface;
465         
466         me->vdata= vdata;
467         me->edata= edata;
468         me->fdata= fdata;
469
470         mesh_update_customdata_pointers(me);
471         
472         /* old material array */
473         for(a=1; a<=ob->totcol; a++) {
474                 ma= ob->mat[a-1];
475                 if(ma) ma->id.us--;
476         }
477         for(a=1; a<=me->totcol; a++) {
478                 ma= me->mat[a-1];
479                 if(ma) ma->id.us--;
480         }
481         if(ob->mat) MEM_freeN(ob->mat);
482         if(ob->matbits) MEM_freeN(ob->matbits);
483         if(me->mat) MEM_freeN(me->mat);
484         ob->mat= me->mat= NULL;
485         ob->matbits= NULL;
486         
487         if(totcol) {
488                 me->mat= matar;
489                 ob->mat= MEM_callocN(sizeof(void *)*totcol, "join obmatar");
490                 ob->matbits= MEM_callocN(sizeof(char)*totcol, "join obmatbits");
491         }
492         else
493                 MEM_freeN(matar);
494         
495         ob->totcol= me->totcol= totcol;
496         ob->colbits= 0;
497
498         if (matmap) MEM_freeN(matmap);
499         
500         /* other mesh users */
501         test_object_materials((ID *)me);
502         
503         /* free temp copy of destination shapekeys (if applicable) */
504         if(nkey) {
505                 // XXX 2.5 Animato
506 #if 0
507                 /* free it's ipo too - both are not actually freed from memory yet as ID-blocks */
508                 if(nkey->ipo) {
509                         free_ipo(nkey->ipo);
510                         BLI_remlink(&G.main->ipo, nkey->ipo);
511                         MEM_freeN(nkey->ipo);
512                 }
513 #endif
514                 
515                 free_key(nkey);
516                 BLI_remlink(&G.main->key, nkey);
517                 MEM_freeN(nkey);
518         }
519         
520         DAG_scene_sort(scene);  // removed objects, need to rebuild dag before editmode call
521         
522         ED_object_enter_editmode(C, EM_WAITCURSOR);
523         ED_object_exit_editmode(C, EM_FREEDATA|EM_WAITCURSOR|EM_DO_UNDO);
524
525         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
526
527         return OPERATOR_FINISHED;
528 }
529
530 /*********************** JOIN AS SHAPES ***************************/
531
532 /* Append selected meshes vertex locations as shapes of the active mesh, 
533   return 0 if no join is made (error) and 1 of the join is done */
534
535 int join_mesh_shapes_exec(bContext *C, wmOperator *op)
536 {
537         Scene *scene= CTX_data_scene(C);
538         Object *ob= CTX_data_active_object(C);
539         Mesh *me= (Mesh *)ob->data;
540         Mesh *selme=NULL;
541         DerivedMesh *dm=NULL;
542         Key *key=me->key;
543         KeyBlock *kb;
544         int ok=0, nonequal_verts=0;
545         
546         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
547                 if (base->object == ob) continue;
548                 
549                 if (base->object->type==OB_MESH) {
550                         selme = (Mesh *)base->object->data;
551                         
552                         if (selme->totvert==me->totvert)
553                                 ok++;
554                         else
555                                 nonequal_verts=1;
556                 }
557         }
558         CTX_DATA_END;
559         
560         if (!ok) {
561                 if (nonequal_verts)
562                         BKE_report(op->reports, RPT_ERROR, "Selected meshes must have equal numbers of vertices.");
563                 else
564                         BKE_report(op->reports, RPT_ERROR, "No additional selected meshes with equal vertex count to join.");
565                 return OPERATOR_CANCELLED;
566         }
567         
568         if(key == NULL) {
569                 key= me->key= add_key((ID *)me);
570                 key->type= KEY_RELATIVE;
571
572                 /* first key added, so it was the basis. initialise it with the existing mesh */
573                 kb= add_keyblock(key, NULL);
574                 mesh_to_key(me, kb);
575         }
576         
577         /* now ready to add new keys from selected meshes */
578         CTX_DATA_BEGIN(C, Base*, base, selected_editable_bases) {
579                 if (base->object == ob) continue;
580                 
581                 if(base->object->type==OB_MESH) {
582                         selme = (Mesh *)base->object->data;
583                         
584                         if (selme->totvert==me->totvert) {
585                                 dm = mesh_get_derived_deform(scene, base->object, CD_MASK_BAREMESH);
586                                 
587                                 if (!dm) continue;
588                                         
589                                 kb= add_keyblock(key, base->object->id.name+2);
590                                 
591                                 DM_to_meshkey(dm, me, kb);
592                                 
593                                 dm->release(dm);
594                         }
595                 }
596         }
597         CTX_DATA_END;
598         
599         WM_event_add_notifier(C, NC_SCENE|ND_OB_ACTIVE, scene);
600         
601         return OPERATOR_FINISHED;
602 }
603
604 /* ********************* MESH VERTEX OCTREE LOOKUP ************* */
605
606 /* important note; this is unfinished, needs better API for editmode, and custom threshold */
607
608 #define MOC_RES                 8
609 #define MOC_NODE_RES    8
610 #define MOC_THRESH              0.00002f
611
612 typedef struct MocNode {
613         struct MocNode *next;
614         intptr_t index[MOC_NODE_RES];
615 } MocNode;
616
617 static int mesh_octree_get_base_offs(float *co, float *offs, float *div)
618 {
619         int vx, vy, vz;
620         
621         vx= floor( (co[0]-offs[0])/div[0] );
622         vy= floor( (co[1]-offs[1])/div[1] );
623         vz= floor( (co[2]-offs[2])/div[2] );
624         
625         CLAMP(vx, 0, MOC_RES-1);
626         CLAMP(vy, 0, MOC_RES-1);
627         CLAMP(vz, 0, MOC_RES-1);
628
629         return (vx*MOC_RES*MOC_RES) + vy*MOC_RES + vz;
630 }
631
632 static void mesh_octree_add_node(MocNode **bt, intptr_t index)
633 {
634         if(*bt==NULL) {
635                 *bt= MEM_callocN(sizeof(MocNode), "MocNode");
636                 (*bt)->index[0]= index;
637         }
638         else {
639                 int a;
640                 for(a=0; a<MOC_NODE_RES; a++) {
641                         if((*bt)->index[a]==index)
642                                 return;
643                         else if((*bt)->index[a]==0) {
644                                 (*bt)->index[a]= index;
645                                 return;
646                         }
647                 }
648                 mesh_octree_add_node(&(*bt)->next, index);
649         }
650 }
651
652 static void mesh_octree_free_node(MocNode **bt)
653 {
654         if( (*bt)->next ) {
655                 mesh_octree_free_node(&(*bt)->next);
656         }
657         MEM_freeN(*bt);
658 }
659
660
661 /* temporal define, just to make nicer code below */
662 #define MOC_ADDNODE(vx, vy, vz) mesh_octree_add_node(basetable + ((vx)*MOC_RES*MOC_RES) + (vy)*MOC_RES + (vz), index)
663
664 static void mesh_octree_add_nodes(MocNode **basetable, float *co, float *offs, float *div, intptr_t index)
665 {
666         float fx, fy, fz;
667         int vx, vy, vz;
668         
669         if (!finite(co[0]) ||
670                 !finite(co[1]) ||
671                 !finite(co[2])
672         ) {
673                 return;
674         }
675         
676         fx= (co[0]-offs[0])/div[0];
677         fy= (co[1]-offs[1])/div[1];
678         fz= (co[2]-offs[2])/div[2];
679         CLAMP(fx, 0.0f, MOC_RES-MOC_THRESH);
680         CLAMP(fy, 0.0f, MOC_RES-MOC_THRESH);
681         CLAMP(fz, 0.0f, MOC_RES-MOC_THRESH);
682         
683         vx= floor(fx);
684         vy= floor(fy);
685         vz= floor(fz);
686         
687         MOC_ADDNODE(vx, vy, vz);
688         
689         if( vx>0 )
690                 if( fx-((float)vx)-MOC_THRESH < 0.0f)
691                         MOC_ADDNODE(vx-1, vy, vz);
692         if( vx<MOC_RES-2 )
693                 if( fx-((float)vx)+MOC_THRESH > 1.0f)
694                         MOC_ADDNODE(vx+1, vy, vz);
695
696         if( vy>0 )
697                 if( fy-((float)vy)-MOC_THRESH < 0.0f) 
698                         MOC_ADDNODE(vx, vy-1, vz);
699         if( vy<MOC_RES-2 )
700                 if( fy-((float)vy)+MOC_THRESH > 1.0f) 
701                         MOC_ADDNODE(vx, vy+1, vz);
702
703         if( vz>0 )
704                 if( fz-((float)vz)-MOC_THRESH < 0.0f) 
705                         MOC_ADDNODE(vx, vy, vz-1);
706         if( vz<MOC_RES-2 )
707                 if( fz-((float)vz)+MOC_THRESH > 1.0f) 
708                         MOC_ADDNODE(vx, vy, vz+1);
709         
710 }
711
712 static intptr_t mesh_octree_find_index(MocNode **bt, MVert *mvert, float *co)
713 {
714         float *vec;
715         int a;
716         
717         if(*bt==NULL)
718                 return -1;
719         
720         for(a=0; a<MOC_NODE_RES; a++) {
721                 if((*bt)->index[a]) {
722                         /* does mesh verts and editmode, code looks potential dangerous, octree should really be filled OK! */
723                         if(mvert) {
724                                 vec= (mvert+(*bt)->index[a]-1)->co;
725                                 if(compare_v3v3(vec, co, MOC_THRESH))
726                                         return (*bt)->index[a]-1;
727                         }
728                         else {
729                                 EditVert *eve= (EditVert *)((*bt)->index[a]);
730                                 if(compare_v3v3(eve->co, co, MOC_THRESH))
731                                         return (*bt)->index[a];
732                         }
733                 }
734                 else return -1;
735         }
736         if( (*bt)->next)
737                 return mesh_octree_find_index(&(*bt)->next, mvert, co);
738         
739         return -1;
740 }
741
742 static struct {
743         MocNode **table;
744         float offs[3], div[3];
745 } MeshOctree = {NULL, {0, 0, 0}, {0, 0, 0}};
746
747 /* mode is 's' start, or 'e' end, or 'u' use */
748 /* if end, ob can be NULL */
749 intptr_t mesh_octree_table(Object *ob, EditMesh *em, float *co, char mode)
750 {
751         MocNode **bt;
752         
753         if(mode=='u') {         /* use table */
754                 if(MeshOctree.table==NULL)
755                         mesh_octree_table(ob, em, NULL, 's');
756            
757                 if(MeshOctree.table) {
758                         Mesh *me= ob->data;
759                         bt= MeshOctree.table + mesh_octree_get_base_offs(co, MeshOctree.offs, MeshOctree.div);
760                         if(em)
761                                 return mesh_octree_find_index(bt, NULL, co);
762                         else
763                                 return mesh_octree_find_index(bt, me->mvert, co);
764                 }
765                 return -1;
766         }
767         else if(mode=='s') {    /* start table */
768                 Mesh *me= ob->data;
769                 float min[3], max[3];
770
771                 /* we compute own bounding box and don't reuse ob->bb because
772                  * we are using the undeformed coordinates*/
773                 INIT_MINMAX(min, max);
774
775                 if(em && me->edit_mesh==em) {
776                         EditVert *eve;
777                         
778                         for(eve= em->verts.first; eve; eve= eve->next)
779                                 DO_MINMAX(eve->co, min, max)
780                 }
781                 else {          
782                         MVert *mvert;
783                         int a;
784                         
785                         for(a=0, mvert= me->mvert; a<me->totvert; a++, mvert++)
786                                 DO_MINMAX(mvert->co, min, max);
787                 }
788                 
789                 /* for quick unit coordinate calculus */
790                 VECCOPY(MeshOctree.offs, min);
791                 MeshOctree.offs[0]-= MOC_THRESH;                /* we offset it 1 threshold unit extra */
792                 MeshOctree.offs[1]-= MOC_THRESH;
793                 MeshOctree.offs[2]-= MOC_THRESH;
794                 
795                 sub_v3_v3v3(MeshOctree.div, max, min);
796                 MeshOctree.div[0]+= 2*MOC_THRESH;       /* and divide with 2 threshold unit more extra (try 8x8 unit grid on paint) */
797                 MeshOctree.div[1]+= 2*MOC_THRESH;
798                 MeshOctree.div[2]+= 2*MOC_THRESH;
799                 
800                 mul_v3_fl(MeshOctree.div, 1.0f/MOC_RES);
801                 if(MeshOctree.div[0]==0.0f) MeshOctree.div[0]= 1.0f;
802                 if(MeshOctree.div[1]==0.0f) MeshOctree.div[1]= 1.0f;
803                 if(MeshOctree.div[2]==0.0f) MeshOctree.div[2]= 1.0f;
804                         
805                 if(MeshOctree.table) /* happens when entering this call without ending it */
806                         mesh_octree_table(ob, em, co, 'e');
807                 
808                 MeshOctree.table= MEM_callocN(MOC_RES*MOC_RES*MOC_RES*sizeof(void *), "sym table");
809                 
810                 if(em && me->edit_mesh==em) {
811                         EditVert *eve;
812
813                         for(eve= em->verts.first; eve; eve= eve->next) {
814                                 mesh_octree_add_nodes(MeshOctree.table, eve->co, MeshOctree.offs, MeshOctree.div, (intptr_t)(eve));
815                         }
816                 }
817                 else {          
818                         MVert *mvert;
819                         int a;
820                         
821                         for(a=0, mvert= me->mvert; a<me->totvert; a++, mvert++)
822                                 mesh_octree_add_nodes(MeshOctree.table, mvert->co, MeshOctree.offs, MeshOctree.div, a+1);
823                 }
824         }
825         else if(mode=='e') { /* end table */
826                 if(MeshOctree.table) {
827                         int a;
828                         
829                         for(a=0, bt=MeshOctree.table; a<MOC_RES*MOC_RES*MOC_RES; a++, bt++) {
830                                 if(*bt) mesh_octree_free_node(bt);
831                         }
832                         MEM_freeN(MeshOctree.table);
833                         MeshOctree.table= NULL;
834                 }
835         }
836         return 0;
837 }
838
839
840 /* ********************* MESH VERTEX MIRR TOPO LOOKUP *************** */
841
842 #define MIRRHASH_TYPE int
843
844 typedef struct MirrTopoPair {
845         long hash;
846         int vIndex;
847 } MirrTopoPair;
848
849 static int MirrTopo_long_sort(const void *l1, const void *l2)
850 {
851         if(                     (MIRRHASH_TYPE)(intptr_t)l1 > (MIRRHASH_TYPE)(intptr_t)l2 ) return  1;
852         else if(        (MIRRHASH_TYPE)(intptr_t)l1 < (MIRRHASH_TYPE)(intptr_t)l2 ) return -1;
853         return 0;
854 }
855
856 static int MirrTopo_item_sort(const void *v1, const void *v2)
857 {
858         if(                     ((MirrTopoPair *)v1)->hash > ((MirrTopoPair *)v2)->hash ) return  1;
859         else if(        ((MirrTopoPair *)v1)->hash < ((MirrTopoPair *)v2)->hash ) return -1;
860         return 0;
861 }
862
863 static long *mesh_topo_lookup = NULL;
864 static int  mesh_topo_lookup_tot = -1;
865 static int  mesh_topo_lookup_mode = -1;
866
867 /* mode is 's' start, or 'e' end, or 'u' use */
868 /* if end, ob can be NULL */
869 long mesh_mirrtopo_table(Object *ob, char mode)
870 {
871         if(mode=='u') {         /* use table */
872                 Mesh *me= ob->data;
873                 if(     (mesh_topo_lookup==NULL) ||
874                         (mesh_topo_lookup_mode != ob->mode) ||
875                         (me->edit_mesh && me->edit_mesh->totvert != mesh_topo_lookup_tot) ||
876                         (me->edit_mesh==NULL && me->totvert != mesh_topo_lookup_tot)
877                 ) {
878                         mesh_mirrtopo_table(ob, 's');
879                 }
880         } else if(mode=='s') { /* start table */
881                 Mesh *me= ob->data;
882                 MEdge *medge;
883                 EditMesh *em= me->edit_mesh;
884
885
886                 /* editmode*/
887                 EditEdge *eed;
888
889                 int a, last, totvert;
890                 int totUnique= -1, totUniqueOld= -1;
891
892                 MIRRHASH_TYPE *MirrTopoHash = NULL;
893                 MIRRHASH_TYPE *MirrTopoHash_Prev = NULL;
894                 MirrTopoPair *MirrTopoPairs;
895                 mesh_topo_lookup_mode= ob->mode;
896
897                 /* reallocate if needed */
898                 if (mesh_topo_lookup) {
899                         MEM_freeN(mesh_topo_lookup);
900                         mesh_topo_lookup = NULL;
901                 }
902
903                 if(em) {
904                         EditVert *eve;
905                         totvert= 0;
906                         for(eve= em->verts.first; eve; eve= eve->next) {
907                                 eve->hash = totvert++;
908                         }
909                 } else {
910                         totvert = me->totvert;
911                 }
912
913                 MirrTopoHash = MEM_callocN( totvert * sizeof(MIRRHASH_TYPE), "TopoMirr" );
914
915                 /* Initialize the vert-edge-user counts used to detect unique topology */
916                 if(em) {
917                         for(eed=em->edges.first; eed; eed= eed->next) {
918                                 MirrTopoHash[eed->v1->hash]++;
919                                 MirrTopoHash[eed->v2->hash]++;
920                         }
921                 } else {
922                         for(a=0, medge=me->medge; a<me->totedge; a++, medge++) {
923                                 MirrTopoHash[medge->v1]++;
924                                 MirrTopoHash[medge->v2]++;
925                         }
926                 }
927
928                 MirrTopoHash_Prev = MEM_dupallocN( MirrTopoHash );
929
930                 totUniqueOld = -1;
931                 while(1) {
932                         /* use the number of edges per vert to give verts unique topology IDs */
933
934                         if(em) {
935                                 for(eed=em->edges.first; eed; eed= eed->next) {
936                                         MirrTopoHash[eed->v1->hash] += MirrTopoHash_Prev[eed->v2->hash];
937                                         MirrTopoHash[eed->v2->hash] += MirrTopoHash_Prev[eed->v1->hash];
938                                 }
939                         } else {
940                                 for(a=0, medge=me->medge; a<me->totedge; a++, medge++) {
941                                         /* This can make realy big numbers, wrapping around here is fine */
942                                         MirrTopoHash[medge->v1] += MirrTopoHash_Prev[medge->v2];
943                                         MirrTopoHash[medge->v2] += MirrTopoHash_Prev[medge->v1];
944                                 }
945                         }
946                         memcpy(MirrTopoHash_Prev, MirrTopoHash, sizeof(MIRRHASH_TYPE) * totvert);
947
948                         /* sort so we can count unique values */
949                         qsort(MirrTopoHash_Prev, totvert, sizeof(MIRRHASH_TYPE), MirrTopo_long_sort);
950
951                         totUnique = 1; /* account for skiping the first value */
952                         for(a=1; a<totvert; a++) {
953                                 if (MirrTopoHash_Prev[a-1] != MirrTopoHash_Prev[a]) {
954                                         totUnique++;
955                                 }
956                         }
957
958                         if (totUnique <= totUniqueOld) {
959                                 /* Finish searching for unique valus when 1 loop dosnt give a
960                                  * higher number of unique values compared to the previous loop */
961                                 break;
962                         } else {
963                                 totUniqueOld = totUnique;
964                         }
965                         /* Copy the hash calculated this iter, so we can use them next time */
966                         memcpy(MirrTopoHash_Prev, MirrTopoHash, sizeof(MIRRHASH_TYPE) * totvert);
967                 }
968
969                 /* Hash/Index pairs are needed for sorting to find index pairs */
970                 MirrTopoPairs= MEM_callocN( sizeof(MirrTopoPair) * totvert, "MirrTopoPairs");
971
972                 /* since we are looping through verts, initialize these values here too */
973                 mesh_topo_lookup = MEM_mallocN( totvert * sizeof(long), "mesh_topo_lookup" );
974
975                 if(em) {
976                         EM_init_index_arrays(em,1,0,0);
977                 }
978
979
980                 for(a=0; a<totvert; a++) {
981                         MirrTopoPairs[a].hash= MirrTopoHash[a];
982                         MirrTopoPairs[a].vIndex = a;
983
984                         /* initialize lookup */
985                         mesh_topo_lookup[a] = -1;
986                 }
987
988                 qsort(MirrTopoPairs, totvert, sizeof(MirrTopoPair), MirrTopo_item_sort);
989
990                 /* Since the loop starts at 2, we must define the last index where the hash's differ */
991                 last = ((totvert >= 2) && (MirrTopoPairs[0].hash == MirrTopoPairs[1].hash)) ? 0 : 1;
992
993                 /* Get the pairs out of the sorted hashes, note, totvert+1 means we can use the previous 2,
994                  * but you cant ever access the last 'a' index of MirrTopoPairs */
995                 for(a=2; a < totvert+1; a++) {
996                         /* printf("I %d %ld %d\n", (a-last), MirrTopoPairs[a  ].hash, MirrTopoPairs[a  ].vIndex ); */
997                         if ((a==totvert) || (MirrTopoPairs[a-1].hash != MirrTopoPairs[a].hash)) {
998                                 if (a-last==2) {
999                                         if(em) {
1000                                                 mesh_topo_lookup[MirrTopoPairs[a-1].vIndex] =   (long)EM_get_vert_for_index(MirrTopoPairs[a-2].vIndex);
1001                                                 mesh_topo_lookup[MirrTopoPairs[a-2].vIndex] =   (long)EM_get_vert_for_index(MirrTopoPairs[a-1].vIndex);
1002                                         } else {
1003                                                 mesh_topo_lookup[MirrTopoPairs[a-1].vIndex] =   MirrTopoPairs[a-2].vIndex;
1004                                                 mesh_topo_lookup[MirrTopoPairs[a-2].vIndex] =   MirrTopoPairs[a-1].vIndex;
1005                                         }
1006                                 }
1007                                 last= a;
1008                         }
1009                 }
1010                 if(em) {
1011                         EM_free_index_arrays();
1012                 }
1013
1014                 MEM_freeN( MirrTopoPairs );
1015                 MirrTopoPairs = NULL;
1016
1017                 MEM_freeN( MirrTopoHash );
1018                 MEM_freeN( MirrTopoHash_Prev );
1019
1020                 mesh_topo_lookup_tot = totvert;
1021
1022         } else if(mode=='e') { /* end table */
1023                 if (mesh_topo_lookup) {
1024                         MEM_freeN(mesh_topo_lookup);
1025                 }
1026                 mesh_topo_lookup = NULL;
1027                 mesh_topo_lookup_tot= -1;
1028         }
1029         return 0;
1030 }
1031
1032 int mesh_get_x_mirror_vert_spacial(Object *ob, int index)
1033 {
1034         Mesh *me= ob->data;
1035         MVert *mvert;
1036         float vec[3];
1037         
1038         mvert= me->mvert+index;
1039         vec[0]= -mvert->co[0];
1040         vec[1]= mvert->co[1];
1041         vec[2]= mvert->co[2];
1042         
1043         return mesh_octree_table(ob, NULL, vec, 'u');
1044 }
1045
1046 static int mesh_get_x_mirror_vert_topo(Object *ob, int index)
1047 {
1048         if (mesh_mirrtopo_table(ob, 'u')==-1)
1049                 return -1;
1050
1051         return mesh_topo_lookup[index];
1052 }
1053
1054 int mesh_get_x_mirror_vert(Object *ob, int index)
1055 {
1056         if (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_TOPO) {
1057                 return mesh_get_x_mirror_vert_topo(ob, index);
1058         } else {
1059                 return mesh_get_x_mirror_vert_spacial(ob, index);
1060         }
1061 }
1062
1063 static EditVert *editmesh_get_x_mirror_vert_spacial(Object *ob, EditMesh *em, float *co)
1064 {
1065         float vec[3];
1066         intptr_t poinval;
1067         
1068         /* ignore nan verts */
1069         if (!finite(co[0]) ||
1070                 !finite(co[1]) ||
1071                 !finite(co[2])
1072            )
1073                 return NULL;
1074         
1075         vec[0]= -co[0];
1076         vec[1]= co[1];
1077         vec[2]= co[2];
1078         
1079         poinval= mesh_octree_table(ob, em, vec, 'u');
1080         if(poinval != -1)
1081                 return (EditVert *)(poinval);
1082         return NULL;
1083 }
1084
1085 static EditVert *editmesh_get_x_mirror_vert_topo(Object *ob, struct EditMesh *em, EditVert *eve, int index)
1086 {
1087         long poinval;
1088         if (mesh_mirrtopo_table(ob, 'u')==-1)
1089                 return NULL;
1090
1091         if (index!=-1) {
1092                 index = BLI_findindex(&em->verts, eve);
1093         }
1094
1095         if (index==-1)
1096                 return NULL;
1097
1098         poinval= mesh_topo_lookup[ index ];
1099
1100         if(poinval != -1)
1101                 return (EditVert *)(poinval);
1102         return NULL;
1103 }
1104
1105 EditVert *editmesh_get_x_mirror_vert(Object *ob, struct EditMesh *em, EditVert *eve, float *co, int index)
1106 {
1107         if (((Mesh *)ob->data)->editflag & ME_EDIT_MIRROR_TOPO) {
1108                 return editmesh_get_x_mirror_vert_topo(ob, em, eve, index);
1109         } else {
1110                 return editmesh_get_x_mirror_vert_spacial(ob, em, eve->co);
1111         }
1112 }
1113
1114
1115 #if 0
1116 float *editmesh_get_mirror_uv(int axis, float *uv, float *mirrCent, float *face_cent)
1117 {
1118         float vec[2];
1119         float cent_vec[2];
1120         float cent[2];
1121
1122         /* ignore nan verts */
1123         if (isnan(uv[0]) || !finite(uv[0]) ||
1124                 isnan(uv[1]) || !finite(uv[1])
1125            )
1126                 return NULL;
1127
1128         if (axis) {
1129                 vec[0]= uv[0];
1130                 vec[1]= -((uv[1])-mirrCent[1]) + mirrCent[1];
1131
1132                 cent_vec[0] = face_cent[0];
1133                 cent_vec[1]= -((face_cent[1])-mirrCent[1]) + mirrCent[1];
1134         } else {
1135                 vec[0]= -((uv[0])-mirrCent[0]) + mirrCent[0];
1136                 vec[1]= uv[1];
1137
1138                 cent_vec[0]= -((face_cent[0])-mirrCent[0]) + mirrCent[0];
1139                 cent_vec[1] = face_cent[1];
1140         }
1141
1142         /* TODO - Optimize */
1143         {
1144                 EditFace *efa;
1145                 int i, len;
1146                 for(efa=em->faces.first; efa; efa=efa->next) {
1147                         MTFace *tf= (MTFace *)CustomData_em_get(&em->fdata, efa->data, CD_MTFACE);
1148                         uv_center(tf->uv, cent, (void *)efa->v4);
1149
1150                         if ( (fabs(cent[0] - cent_vec[0]) < 0.001) && (fabs(cent[1] - cent_vec[1]) < 0.001) ) {
1151                                 len = efa->v4 ? 4 : 3;
1152                                 for (i=0; i<len; i++) {
1153                                         if ( (fabs(tf->uv[i][0] - vec[0]) < 0.001) && (fabs(tf->uv[i][1] - vec[1]) < 0.001) ) {
1154                                                 return tf->uv[i];
1155                                         }
1156                                 }
1157                         }
1158                 }
1159         }
1160
1161         return NULL;
1162 }
1163 #endif
1164
1165 static unsigned int mirror_facehash(void *ptr)
1166 {
1167         MFace *mf= ptr;
1168         int v0, v1;
1169
1170         if(mf->v4) {
1171                 v0= MIN4(mf->v1, mf->v2, mf->v3, mf->v4);
1172                 v1= MAX4(mf->v1, mf->v2, mf->v3, mf->v4);
1173         }
1174         else {
1175                 v0= MIN3(mf->v1, mf->v2, mf->v3);
1176                 v1= MAX3(mf->v1, mf->v2, mf->v3);
1177         }
1178
1179         return ((v0*39)^(v1*31));
1180 }
1181
1182 static int mirror_facerotation(MFace *a, MFace *b)
1183 {
1184         if(b->v4) {
1185                 if(a->v1==b->v1 && a->v2==b->v2 && a->v3==b->v3 && a->v4==b->v4)
1186                         return 0;
1187                 else if(a->v4==b->v1 && a->v1==b->v2 && a->v2==b->v3 && a->v3==b->v4)
1188                         return 1;
1189                 else if(a->v3==b->v1 && a->v4==b->v2 && a->v1==b->v3 && a->v2==b->v4)
1190                         return 2;
1191                 else if(a->v2==b->v1 && a->v3==b->v2 && a->v4==b->v3 && a->v1==b->v4)
1192                         return 3;
1193         }
1194         else {
1195                 if(a->v1==b->v1 && a->v2==b->v2 && a->v3==b->v3)
1196                         return 0;
1197                 else if(a->v3==b->v1 && a->v1==b->v2 && a->v2==b->v3)
1198                         return 1;
1199                 else if(a->v2==b->v1 && a->v3==b->v2 && a->v1==b->v3)
1200                         return 2;
1201         }
1202         
1203         return -1;
1204 }
1205
1206 static int mirror_facecmp(void *a, void *b)
1207 {
1208         return (mirror_facerotation((MFace*)a, (MFace*)b) == -1);
1209 }
1210
1211 int *mesh_get_x_mirror_faces(Object *ob, EditMesh *em)
1212 {
1213         Mesh *me= ob->data;
1214         MVert *mv, *mvert= me->mvert;
1215         MFace mirrormf, *mf, *hashmf, *mface= me->mface;
1216         GHash *fhash;
1217         int *mirrorverts, *mirrorfaces;
1218         int a;
1219
1220         mirrorverts= MEM_callocN(sizeof(int)*me->totvert, "MirrorVerts");
1221         mirrorfaces= MEM_callocN(sizeof(int)*2*me->totface, "MirrorFaces");
1222
1223         mesh_octree_table(ob, em, NULL, 's');
1224
1225         for(a=0, mv=mvert; a<me->totvert; a++, mv++)
1226                 mirrorverts[a]= mesh_get_x_mirror_vert(ob, a);
1227
1228         mesh_octree_table(ob, em, NULL, 'e');
1229
1230         fhash= BLI_ghash_new(mirror_facehash, mirror_facecmp, "mirror_facehash gh");
1231         for(a=0, mf=mface; a<me->totface; a++, mf++)
1232                 BLI_ghash_insert(fhash, mf, mf);
1233
1234         for(a=0, mf=mface; a<me->totface; a++, mf++) {
1235                 mirrormf.v1= mirrorverts[mf->v3];
1236                 mirrormf.v2= mirrorverts[mf->v2];
1237                 mirrormf.v3= mirrorverts[mf->v1];
1238                 mirrormf.v4= (mf->v4)? mirrorverts[mf->v4]: 0;
1239
1240                 /* make sure v4 is not 0 if a quad */
1241                 if(mf->v4 && mirrormf.v4==0) {
1242                         SWAP(int, mirrormf.v1, mirrormf.v3);
1243                         SWAP(int, mirrormf.v2, mirrormf.v4);
1244                 }
1245
1246                 hashmf= BLI_ghash_lookup(fhash, &mirrormf);
1247                 if(hashmf) {
1248                         mirrorfaces[a*2]= hashmf - mface;
1249                         mirrorfaces[a*2+1]= mirror_facerotation(&mirrormf, hashmf);
1250                 }
1251                 else
1252                         mirrorfaces[a*2]= -1;
1253         }
1254
1255         BLI_ghash_free(fhash, NULL, NULL);
1256         MEM_freeN(mirrorverts);
1257         
1258         return mirrorfaces;
1259 }