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