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