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