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