split BKE_utildefines.h, now it only has blender specific defines like GS() MAKE_ID...
[blender.git] / source / blender / blenkernel / intern / BME_conversions.c
1 /**
2  * BME_mesh.c    jan 2007
3  *
4  *      BMesh mesh level functions.
5  *
6  * $Id$
7  *
8  * ***** BEGIN GPL LICENSE BLOCK *****
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  * about this.  
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
24  *
25  * The Original Code is Copyright (C) 2007 Blender Foundation.
26  * All rights reserved.
27  *
28  * The Original Code is: all of this file.
29  *
30  * Contributor(s): Geoffrey Bantle, Levi Schooley.
31  *
32  * ***** END GPL LICENSE BLOCK *****
33  */
34
35 #include "MEM_guardedalloc.h"
36
37 #include "DNA_meshdata_types.h"
38 #include "DNA_object_types.h"
39 #include "DNA_scene_types.h"
40
41 #include "BLI_edgehash.h"
42 #include "BLI_utildefines.h"
43
44 #include "BKE_mesh.h"
45 #include "BKE_cdderivedmesh.h"
46
47 //XXX #include "BIF_editmesh.h"
48 //XXX #include "editmesh.h"
49 #include "bmesh_private.h"
50
51 //XXX #include "BSE_edit.h"
52
53 /* XXX IMPORTANT: editmesh stuff doesn't belong in kernel! (ton) */
54
55 /*merge these functions*/
56 static void BME_DMcorners_to_loops(BME_Mesh *bm, CustomData *facedata, int index, BME_Poly *f, int numCol, int numTex){
57         int i, j;
58         BME_Loop *l;
59         MTFace *texface;
60         MTexPoly *texpoly;
61         MCol *mcol;
62         MLoopCol *mloopcol;
63         MLoopUV *mloopuv;
64
65         for(i=0; i< numTex; i++){
66                 texface = CustomData_get_layer_n(facedata, CD_MTFACE, i);
67                 texpoly = CustomData_bmesh_get_n(&bm->pdata, f->data, CD_MTEXPOLY, i);
68
69                 texpoly->tpage = texface[index].tpage;
70                 texpoly->flag = texface[index].flag;
71                 texpoly->transp = texface[index].transp;
72                 texpoly->mode = texface[index].mode;
73                 texpoly->tile = texface[index].tile;
74                 texpoly->unwrap = texface[index].unwrap;
75
76                 j = 0;
77                 l = f->loopbase;
78                 do{
79                         mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->data, CD_MLOOPUV, i);
80                         mloopuv->uv[0] = texface[index].uv[j][0];
81                         mloopuv->uv[1] = texface[index].uv[j][1];
82                         j++;
83                         l = l->next;
84                 }while(l!=f->loopbase);
85         }
86
87         for(i=0; i < numCol; i++){
88                 mcol = CustomData_get_layer_n(facedata, CD_MCOL, i);
89                 j = 0;
90                 l = f->loopbase;
91                 do{
92                         mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->data, CD_MLOOPCOL, i);
93                         mloopcol->r = mcol[(index*4)+j].r;
94                         mloopcol->g = mcol[(index*4)+j].g;
95                         mloopcol->b = mcol[(index*4)+j].b;
96                         mloopcol->a = mcol[(index*4)+j].a;
97                         j++;
98                         l = l->next;
99                 }while(l!=f->loopbase);
100         }
101 }
102
103 static void BME_DMloops_to_corners(BME_Mesh *bm, CustomData *facedata, int index, BME_Poly *f,int numCol, int numTex){
104         int i, j;
105         BME_Loop *l;
106         MTFace *texface;
107         MTexPoly *texpoly;
108         MCol *mcol;
109         MLoopCol *mloopcol;
110         MLoopUV *mloopuv;
111
112         for(i=0; i < numTex; i++){
113                 texface = CustomData_get_layer_n(facedata, CD_MTFACE, i);
114                 texpoly = CustomData_bmesh_get_n(&bm->pdata, f->data, CD_MTEXPOLY, i);
115                 
116                 texface[index].tpage = texpoly->tpage;
117                 texface[index].flag = texpoly->flag;
118                 texface[index].transp = texpoly->transp;
119                 texface[index].mode = texpoly->mode;
120                 texface[index].tile = texpoly->tile;
121                 texface[index].unwrap = texpoly->unwrap;
122
123                 j = 0;
124                 l = f->loopbase;
125                 do{
126                         mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->data, CD_MLOOPUV, i);
127                         texface[index].uv[j][0] = mloopuv->uv[0];
128                         texface[index].uv[j][1] = mloopuv->uv[1];
129                         j++;
130                         l = l->next;
131                 }while(l!=f->loopbase);
132
133         }
134         for(i=0; i < numCol; i++){
135                 mcol = CustomData_get_layer_n(facedata,CD_MCOL, i);
136                 j = 0;
137                 l = f->loopbase;
138                 do{
139                         mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->data, CD_MLOOPCOL, i);
140                         mcol[(index*4) + j].r = mloopcol->r;
141                         mcol[(index*4) + j].g = mloopcol->g;
142                         mcol[(index*4) + j].b = mloopcol->b;
143                         mcol[(index*4) + j].a = mloopcol->a;
144                         j++;
145                         l = l->next;
146                 }while(l!=f->loopbase);
147         }
148 }
149
150
151 static void BME_corners_to_loops(BME_Mesh *bm, CustomData *facedata, void *face_block, BME_Poly *f,int numCol, int numTex){
152         int i, j;
153         BME_Loop *l;
154         MTFace *texface;
155         MTexPoly *texpoly;
156         MCol *mcol;
157         MLoopCol *mloopcol;
158         MLoopUV *mloopuv;
159
160         for(i=0; i < numTex; i++){
161                 texface = CustomData_em_get_n(facedata, face_block, CD_MTFACE, i);
162                 texpoly = CustomData_bmesh_get_n(&bm->pdata, f->data, CD_MTEXPOLY, i);
163                 
164                 texpoly->tpage = texface->tpage;
165                 texpoly->flag = texface->flag;
166                 texpoly->transp = texface->transp;
167                 texpoly->mode = texface->mode;
168                 texpoly->tile = texface->tile;
169                 texpoly->unwrap = texface->unwrap;
170
171                 j = 0;
172                 l = f->loopbase;
173                 do{
174                         mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->data, CD_MLOOPUV, i);
175                         mloopuv->uv[0] = texface->uv[j][0];
176                         mloopuv->uv[1] = texface->uv[j][1];
177                         j++;
178                         l = l->next;
179                 }while(l!=f->loopbase);
180
181         }
182         for(i=0; i < numCol; i++){
183                 mcol = CustomData_em_get_n(facedata, face_block, CD_MCOL, i);
184                 j = 0;
185                 l = f->loopbase;
186                 do{
187                         mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->data, CD_MLOOPCOL, i);
188                         mloopcol->r = mcol[j].r;
189                         mloopcol->g = mcol[j].g;
190                         mloopcol->b = mcol[j].b;
191                         mloopcol->a = mcol[j].a;
192                         j++;
193                         l = l->next;
194                 }while(l!=f->loopbase);
195         }
196 }
197
198 static void BME_loops_to_corners(BME_Mesh *bm, CustomData *facedata, void *face_block, BME_Poly *f,int numCol, int numTex){
199         int i, j;
200         BME_Loop *l;
201         MTFace *texface;
202         MTexPoly *texpoly;
203         MCol *mcol;
204         MLoopCol *mloopcol;
205         MLoopUV *mloopuv;
206
207         for(i=0; i < numTex; i++){
208                 texface = CustomData_em_get_n(facedata, face_block, CD_MTFACE, i);
209                 texpoly = CustomData_bmesh_get_n(&bm->pdata, f->data, CD_MTEXPOLY, i);
210                 
211                 texface->tpage = texpoly->tpage;
212                 texface->flag = texpoly->flag;
213                 texface->transp = texpoly->transp;
214                 texface->mode = texpoly->mode;
215                 texface->tile = texpoly->tile;
216                 texface->unwrap = texpoly->unwrap;
217
218                 j = 0;
219                 l = f->loopbase;
220                 do{
221                         mloopuv = CustomData_bmesh_get_n(&bm->ldata, l->data, CD_MLOOPUV, i);
222                         texface->uv[j][0] = mloopuv->uv[0];
223                         texface->uv[j][1] = mloopuv->uv[1];
224                         j++;
225                         l = l->next;
226                 }while(l!=f->loopbase);
227
228         }
229         for(i=0; i < numCol; i++){
230                 mcol = CustomData_em_get_n(facedata, face_block, CD_MCOL, i);
231                 j = 0;
232                 l = f->loopbase;
233                 do{
234                         mloopcol = CustomData_bmesh_get_n(&bm->ldata, l->data, CD_MLOOPCOL, i);
235                         mcol[j].r = mloopcol->r;
236                         mcol[j].g = mloopcol->g;
237                         mcol[j].b = mloopcol->b;
238                         mcol[j].a = mloopcol->a;
239                         j++;
240                         l = l->next;
241                 }while(l!=f->loopbase);
242         }
243 }
244 /*move the EditMesh conversion functions to editmesh_tools.c*/
245 BME_Mesh *BME_editmesh_to_bmesh(EditMesh *em) {
246         BME_Mesh *bm;
247         int allocsize[4] = {512,512,2048,512}, numTex, numCol;
248         BME_Vert *v1, *v2;
249         BME_Edge *e, *edar[4];
250         BME_Poly *f;
251
252         EditVert *eve;
253         EditEdge *eed;
254         EditFace *efa;
255
256         int len;
257         bm = BME_make_mesh(allocsize);
258
259         /*copy custom data layout*/
260         CustomData_copy(&em->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
261         CustomData_copy(&em->edata, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
262         CustomData_copy(&em->fdata, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
263
264         /*copy face corner data*/
265         CustomData_to_bmeshpoly(&em->fdata, &bm->pdata, &bm->ldata);
266         /*initialize memory pools*/
267         CustomData_bmesh_init_pool(&bm->vdata, allocsize[0]);
268         CustomData_bmesh_init_pool(&bm->edata, allocsize[1]);
269         CustomData_bmesh_init_pool(&bm->ldata, allocsize[2]);
270         CustomData_bmesh_init_pool(&bm->pdata, allocsize[3]);
271         /*needed later*/
272         numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
273         numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
274
275         BME_model_begin(bm);
276         /*add verts*/
277         eve= em->verts.first;
278         while(eve) {
279                 v1 = BME_MV(bm,eve->co);
280                 VECCOPY(v1->no,eve->no);
281                 v1->flag = eve->f;
282                 v1->h = eve->h;
283                 v1->bweight = eve->bweight;
284                 /*Copy Custom Data*/
285                 CustomData_bmesh_copy_data(&em->vdata, &bm->vdata, eve->data, &v1->data);
286                 eve->tmp.v = (EditVert*)v1;
287                 eve = eve->next;
288         }
289         
290         /*add edges*/
291         eed= em->edges.first;
292         while(eed) {
293                 v1 = (BME_Vert*)eed->v1->tmp.v;
294                 v2 = (BME_Vert*)eed->v2->tmp.v;
295                 e = BME_ME(bm, v1, v2);
296                 e->crease = eed->crease;
297                 e->bweight = eed->bweight;
298                 e->flag = eed->f & SELECT;
299                 if(eed->sharp) e->flag |= ME_SHARP;
300                 if(eed->seam) e->flag |= ME_SEAM;
301                 //XXX if(eed->h & EM_FGON) e->flag |= ME_FGON;
302                 if(eed->h & 1) e->flag |= ME_HIDE;
303                 eed->tmp.e = (EditEdge*)e;
304                 CustomData_bmesh_copy_data(&em->edata, &bm->edata, eed->data, &e->data);
305                 eed = eed->next;
306         }
307         /*add faces.*/
308         efa= em->faces.first;
309         while(efa) {
310                 if(efa->v4) len = 4;
311                 else len = 3;
312                 
313                 edar[0] = (BME_Edge*)efa->e1->tmp.e;
314                 edar[1] = (BME_Edge*)efa->e2->tmp.e;
315                 edar[2] = (BME_Edge*)efa->e3->tmp.e;
316                 if(len == 4){
317                         edar[3] = (BME_Edge*)efa->e4->tmp.e;
318                 }
319                 
320                 /*find v1 and v2*/
321                 v1 = (BME_Vert*)efa->v1->tmp.v;
322                 v2 = (BME_Vert*)efa->v2->tmp.v;
323                 
324                 f = BME_MF(bm,v1,v2,edar,len);
325                 f->mat_nr = efa->mat_nr;
326                 f->flag = efa->flag;
327                 if(efa->h) {
328                         f->flag |= ME_HIDE;
329                         f->flag &= ~ME_FACE_SEL;
330                 }
331                 else {
332                         if(efa->f & 1) f->flag |= ME_FACE_SEL;
333                         else f->flag &= ~ME_FACE_SEL;
334                 }
335                 CustomData_bmesh_copy_data(&em->fdata, &bm->pdata, efa->data, &f->data);
336                 BME_corners_to_loops(bm, &em->fdata, efa->data, f,numCol,numTex);
337                 efa = efa->next;
338         }
339         BME_model_end(bm);
340         return bm;
341 }
342 /* adds the geometry in the bmesh to editMesh (does not free editMesh)
343  * if td != NULL, the transdata will be mapped to the EditVert's co */
344 void BME_bmesh_to_editmesh(BME_Mesh *bm, BME_TransData_Head *td, EditMesh *em) {
345         BME_Vert *v1;
346         BME_Edge *e;
347         BME_Poly *f;
348         
349         BME_TransData *vtd;
350
351         EditVert *eve1, *eve2, *eve3, *eve4, **evlist;
352         EditEdge *eed;
353         EditFace *efa;
354
355         int totvert, len, i, numTex, numCol;
356
357         if (em == NULL) return;
358
359         CustomData_copy(&bm->vdata, &em->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
360         CustomData_copy(&bm->edata, &em->edata, CD_MASK_BMESH, CD_CALLOC, 0);
361         CustomData_copy(&bm->pdata, &em->fdata, CD_MASK_BMESH, CD_CALLOC, 0);
362         CustomData_from_bmeshpoly(&em->fdata, &bm->pdata, &bm->ldata,0);
363         numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
364         numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
365
366
367         /* convert to EditMesh */
368         /* make editverts */
369         totvert = BLI_countlist(&(bm->verts));
370         evlist= (EditVert **)MEM_mallocN(totvert*sizeof(void *),"evlist");
371         for (i=0,v1=bm->verts.first;v1;v1=v1->next,i++) {
372                 v1->tflag1 = i;
373                 eve1 = NULL; //XXX addvertlist(v1->co,NULL);
374                 if (td && (vtd = BME_get_transdata(td,v1))) {
375                         vtd->loc = eve1->co;
376                 }
377                 eve1->keyindex = i;
378                 evlist[i]= eve1;
379                 eve1->f = (unsigned char)v1->flag;
380                 eve1->h = (unsigned char)v1->h;
381                 eve1->bweight = v1->bweight;
382                 CustomData_em_copy_data(&bm->vdata, &em->vdata, v1->data, &eve1->data);
383         }
384         
385         /* make edges */
386         for (e=bm->edges.first;e;e=e->next) {
387                 if(0) { //XXX if(!(findedgelist(evlist[e->v1->tflag1], evlist[e->v2->tflag1]))){
388                         eed= NULL; //XXX addedgelist(evlist[e->v1->tflag1], evlist[e->v2->tflag1], NULL);
389                         eed->crease = e->crease;
390                         eed->bweight = e->bweight;
391                         if(e->flag & ME_SEAM) eed->seam = 1;
392                         if(e->flag & ME_SHARP) eed->sharp = 1;
393                         if(e->flag & SELECT) eed->f |= SELECT;
394                         //XXX if(e->flag & ME_FGON) eed->h= EM_FGON; // 2 different defines!
395                         if(e->flag & ME_HIDE) eed->h |= 1;
396                         if(em->selectmode==SCE_SELECT_EDGE) { 
397                                 ; //XXX EM_select_edge(eed, eed->f & SELECT);
398                         }
399                         CustomData_em_copy_data(&bm->edata, &em->edata, e->data, &eed->data);
400                 }
401         }
402
403         /* make faces */
404         for (f=bm->polys.first;f;f=f->next) {
405                 len = BME_cycle_length(f->loopbase);
406                 if (len==3 || len==4) {
407                         eve1= evlist[f->loopbase->v->tflag1];
408                         eve2= evlist[f->loopbase->next->v->tflag1];
409                         eve3= evlist[f->loopbase->next->next->v->tflag1];
410                         if (len == 4) {
411                                 eve4= evlist[f->loopbase->prev->v->tflag1];
412                         }
413                         else {
414                                 eve4= NULL;
415                         }
416
417                         efa = NULL; //XXX addfacelist(eve1, eve2, eve3, eve4, NULL, NULL);
418                         efa->mat_nr = (unsigned char)f->mat_nr;
419                         efa->flag= f->flag & ~ME_HIDE;
420                         if(f->flag & ME_FACE_SEL) {
421                                 efa->f |= SELECT;
422                         }
423                         if(f->flag & ME_HIDE) efa->h= 1;
424                         // XXX flag depricated
425                         // if((G.f & G_FACESELECT) && (efa->f & SELECT))
426                                 //XXX EM_select_face(efa, 1); /* flush down */
427                         CustomData_em_copy_data(&bm->pdata, &em->fdata, f->data, &efa->data);
428                         BME_loops_to_corners(bm, &em->fdata, efa->data, f,numCol,numTex);
429                 }
430         }
431
432         MEM_freeN(evlist);
433
434 }
435
436 /* Adds the geometry found in dm to bm
437   */
438 BME_Mesh *BME_derivedmesh_to_bmesh(DerivedMesh *dm)
439 {
440         
441         BME_Mesh *bm;
442         int allocsize[4] = {512,512,2048,512};
443         MVert *mvert, *mv;
444         MEdge *medge, *me;
445         MFace *mface, *mf;
446         int totface,totedge,totvert,i,len, numTex, numCol;
447         BME_Vert *v1=NULL,*v2=NULL, **vert_array;
448         BME_Edge *e=NULL;
449         BME_Poly *f=NULL;
450         
451         EdgeHash *edge_hash = BLI_edgehash_new();
452
453         bm = BME_make_mesh(allocsize);
454         /*copy custom data layout*/
455         CustomData_copy(&dm->vertData, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
456         CustomData_copy(&dm->edgeData, &bm->edata, CD_MASK_BMESH, CD_CALLOC, 0);
457         CustomData_copy(&dm->faceData, &bm->pdata, CD_MASK_BMESH, CD_CALLOC, 0);
458
459         /*copy face corner data*/
460         CustomData_to_bmeshpoly(&dm->faceData, &bm->pdata, &bm->ldata);
461         /*initialize memory pools*/
462         CustomData_bmesh_init_pool(&bm->vdata, allocsize[0]);
463         CustomData_bmesh_init_pool(&bm->edata, allocsize[1]);
464         CustomData_bmesh_init_pool(&bm->ldata, allocsize[2]);
465         CustomData_bmesh_init_pool(&bm->pdata, allocsize[3]);
466         /*needed later*/
467         numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
468         numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
469
470         totvert = dm->getNumVerts(dm);
471         totedge = dm->getNumEdges(dm);
472         totface = dm->getNumFaces(dm);
473         mvert = dm->getVertArray(dm);
474         medge = dm->getEdgeArray(dm);
475         mface = dm->getFaceArray(dm);
476
477         vert_array = MEM_mallocN(sizeof(*vert_array)*totvert,"BME_derivedmesh_to_bmesh BME_Vert* array");
478
479         BME_model_begin(bm);
480         /*add verts*/
481         for(i=0,mv = mvert; i < totvert;i++,mv++){
482                 v1 = BME_MV(bm,mv->co);
483                 vert_array[i] = v1;
484                 v1->flag = mv->flag;
485                 v1->bweight = mv->bweight/255.0f;
486                 CustomData_to_bmesh_block(&dm->vertData, &bm->vdata, i, &v1->data);
487         }
488         /*add edges*/
489         for(i=0,me = medge; i < totedge;i++,me++){
490                 v1 = vert_array[me->v1];
491                 v2 = vert_array[me->v2];
492                 e = BME_ME(bm, v1, v2);
493                 e->crease = me->crease/255.0f;
494                 e->bweight = me->bweight/255.0f;
495                 e->flag = (unsigned char)me->flag;
496                 BLI_edgehash_insert(edge_hash,me->v1,me->v2,e);
497                 CustomData_to_bmesh_block(&dm->edgeData, &bm->edata, i, &e->data);
498         }
499         /*add faces.*/
500         for(i=0,mf = mface; i < totface;i++,mf++){
501                 BME_Edge *edar[4];
502                 if(mf->v4) len = 4;
503                 else len = 3;
504                 
505                 edar[0] = BLI_edgehash_lookup(edge_hash,mf->v1,mf->v2);
506                 edar[1] = BLI_edgehash_lookup(edge_hash,mf->v2,mf->v3);
507                 if(len == 4){
508                         edar[2] = BLI_edgehash_lookup(edge_hash,mf->v3,mf->v4);
509                         edar[3] = BLI_edgehash_lookup(edge_hash,mf->v4,mf->v1);
510                 }
511                 else
512                         edar[2] = BLI_edgehash_lookup(edge_hash,mf->v3,mf->v1);
513                 
514                 /*find v1 and v2*/
515                 v1 = vert_array[mf->v1];
516                 v2 = vert_array[mf->v2];
517                 
518                 f = BME_MF(bm,v1,v2,edar,len);
519                 f->mat_nr = mf->mat_nr;
520                 f->flag = mf->flag;
521                 CustomData_to_bmesh_block(&dm->faceData,&bm->pdata,i,&f->data);
522                 BME_DMcorners_to_loops(bm, &dm->faceData,i,f, numCol,numTex);
523         }
524         
525         BME_model_end(bm);
526         BLI_edgehash_free(edge_hash, NULL);
527         MEM_freeN(vert_array);
528         return bm;
529 }
530
531 DerivedMesh *BME_bmesh_to_derivedmesh(BME_Mesh *bm, DerivedMesh *dm)
532 {
533         MFace *mface, *mf;
534         MEdge *medge, *me;
535         MVert *mvert, *mv;
536         int *origindex;
537         int totface,totedge,totvert,i,bmeshok,len, numTex, numCol;
538
539         BME_Vert *v1=NULL;
540         BME_Edge *e=NULL, *oe=NULL;
541         BME_Poly *f=NULL;
542         
543         DerivedMesh *result;
544         EdgeHash *edge_hash = BLI_edgehash_new();
545
546         totvert = BLI_countlist(&(bm->verts));
547         totedge = 0;
548         
549         /*we cannot have double edges in a derived mesh!*/
550         for(i=0, v1=bm->verts.first; v1; v1=v1->next, i++) v1->tflag1 = i;
551         for(e=bm->edges.first; e; e=e->next){
552                 oe = BLI_edgehash_lookup(edge_hash,e->v1->tflag1, e->v2->tflag1);
553                 if(!oe){
554                         totedge++;
555                         BLI_edgehash_insert(edge_hash,e->v1->tflag1,e->v2->tflag1,e);
556                         e->tflag2 = 1;
557                 }
558                 else{
559                         e->tflag2 = 0;
560                 }
561         }
562         
563         /*count quads and tris*/
564         totface = 0;
565         bmeshok = 1;
566         for(f=bm->polys.first;f;f=f->next){
567                 len = BME_cycle_length(f->loopbase);
568                 if(len == 3 || len == 4) totface++;
569         }
570         
571         /*convert back to mesh*/
572         result = CDDM_from_template(dm,totvert,totedge,totface);
573         CustomData_merge(&bm->vdata, &result->vertData, CD_MASK_BMESH, CD_CALLOC, totvert);
574         CustomData_merge(&bm->edata, &result->edgeData, CD_MASK_BMESH, CD_CALLOC, totedge);
575         CustomData_merge(&bm->pdata, &result->faceData, CD_MASK_BMESH, CD_CALLOC, totface);
576         CustomData_from_bmeshpoly(&result->faceData, &bm->pdata, &bm->ldata,totface);
577         numTex = CustomData_number_of_layers(&bm->pdata, CD_MTEXPOLY);
578         numCol = CustomData_number_of_layers(&bm->ldata, CD_MLOOPCOL);
579
580
581         /*Make Verts*/
582         mvert = CDDM_get_verts(result);
583         origindex = result->getVertDataArray(result, CD_ORIGINDEX);
584         for(i=0,v1=bm->verts.first,mv=mvert;v1;v1=v1->next,i++,mv++){
585                 VECCOPY(mv->co,v1->co);
586                 mv->flag = (unsigned char)v1->flag;
587                 mv->bweight = (char)(255.0*v1->bweight);
588                 CustomData_from_bmesh_block(&bm->vdata, &result->vertData, &v1->data, i);
589                 origindex[i] = ORIGINDEX_NONE;
590         }
591         medge = CDDM_get_edges(result);
592         origindex = result->getEdgeDataArray(result, CD_ORIGINDEX);
593         i=0;
594         for(e=bm->edges.first,me=medge;e;e=e->next){
595                 if(e->tflag2){
596                         if(e->v1->tflag1 < e->v2->tflag1){
597                                 me->v1 = e->v1->tflag1;
598                                 me->v2 = e->v2->tflag1;
599                         }
600                         else{
601                                 me->v1 = e->v2->tflag1;
602                                 me->v2 = e->v1->tflag1;
603                         }
604                 
605                         me->crease = (char)(255.0*e->crease);
606                         me->bweight = (char)(255.0*e->bweight);
607                         me->flag = e->flag;
608                         CustomData_from_bmesh_block(&bm->edata, &result->edgeData, &e->data, i);
609                         origindex[i] = ORIGINDEX_NONE;
610                         me++;
611                         i++;
612                 }
613         }
614         if(totface){
615                 mface = CDDM_get_faces(result);
616                 origindex = result->getFaceDataArray(result, CD_ORIGINDEX);
617                 /*make faces*/
618                 for(i=0,f=bm->polys.first;f;f=f->next){
619                         mf = &mface[i];
620                         len = BME_cycle_length(f->loopbase);
621                         if(len==3 || len==4){
622                                 mf->v1 = f->loopbase->v->tflag1;
623                                 mf->v2 = f->loopbase->next->v->tflag1;
624                                 mf->v3 = f->loopbase->next->next->v->tflag1;
625                                 if(len == 4){
626                                         mf->v4 = f->loopbase->prev->v->tflag1;
627                                 }
628                                 /* test and rotate indexes if necessary so that verts 3 and 4 aren't index 0 */
629                                 if(mf->v3 == 0 || (len == 4 && mf->v4 == 0)){
630                                         test_index_face(mf, NULL, i, len);
631                                 }
632                                 mf->mat_nr = (unsigned char)f->mat_nr;
633                                 mf->flag = (unsigned char)f->flag;
634                                 CustomData_from_bmesh_block(&bm->pdata, &result->faceData, &f->data, i);
635                                 BME_DMloops_to_corners(bm, &result->faceData, i, f,numCol,numTex);
636                                 origindex[i] = ORIGINDEX_NONE;
637                                 i++;
638                         }
639                 }
640         }
641         BLI_edgehash_free(edge_hash, NULL);
642         return result;
643 }