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