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