-> Vertex Group support for bevel (editmode only)
[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: BME_eulers.c,v 1.00 2007/01/17 17:42:01 Briggs Exp $
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 #include "BIF_editmesh.h"
54 #include "editmesh.h"
55 #include "bmesh_private.h"
56
57 #include "BSE_edit.h"
58 /*Converts an EditMesh to a BME_Mesh.*/
59 static void bmesh_init_cdPool(CustomData *data, int allocsize){
60         if(data->totlayer)data->pool = BLI_mempool_create(data->totsize, allocsize, allocsize);
61 }
62
63 BME_Mesh *BME_editmesh_to_bmesh(EditMesh *em) {
64         BME_Mesh *bm;
65         int allocsize[4] = {512,512,2048,512};
66         BME_Vert *v1, *v2;
67         BME_Edge *e, *edar[4];
68         BME_Poly *f;
69
70         EditVert *eve;
71         EditEdge *eed;
72         EditFace *efa;
73
74         int len;
75         bm = BME_make_mesh(allocsize);
76
77         CustomData_copy(&em->vdata, &bm->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
78         bmesh_init_cdPool(&bm->vdata, allocsize[0]);
79
80         BME_model_begin(bm);
81         /*add verts*/
82         eve= em->verts.first;
83         while(eve) {
84                 v1 = BME_MV(bm,eve->co);
85                 VECCOPY(v1->no,eve->no);
86                 v1->flag = eve->f;
87                 v1->h = eve->h;
88                 v1->bweight = eve->bweight;
89
90                 /*Copy Custom Data*/
91                 CustomData_bmesh_copy_data(&em->vdata, &bm->vdata, eve->data, &v1->data);
92                 eve->tmp.v = (EditVert*)v1;
93                 eve = eve->next;
94         }
95         
96         /*add edges*/
97         eed= em->edges.first;
98         while(eed) {
99                 v1 = (BME_Vert*)eed->v1->tmp.v;
100                 v2 = (BME_Vert*)eed->v2->tmp.v;
101                 e = BME_ME(bm, v1, v2);
102                 e->crease = eed->crease;
103                 e->bweight = eed->bweight;
104                 e->flag = eed->f & SELECT;
105                 if(eed->sharp) e->flag |= ME_SHARP;
106                 if(eed->seam) e->flag |= ME_SEAM;
107                 if(eed->h & EM_FGON) e->flag |= ME_FGON;
108                 if(eed->h & 1) e->flag |= ME_HIDE;
109
110                 /* link the edges for face construction;
111                  * kind of a dangerous thing - remember to cast back to BME_Edge before using! */
112                 /*Copy CustomData*/
113
114                 eed->tmp.e = (EditEdge*)e;
115                 eed = eed->next;
116         }
117
118         /*add faces.*/
119         efa= em->faces.first;
120         while(efa) {
121                 if(efa->v4) len = 4;
122                 else len = 3;
123                 
124                 edar[0] = (BME_Edge*)efa->e1->tmp.e;
125                 edar[1] = (BME_Edge*)efa->e2->tmp.e;
126                 edar[2] = (BME_Edge*)efa->e3->tmp.e;
127                 if(len == 4){
128                         edar[3] = (BME_Edge*)efa->e4->tmp.e;
129                 }
130                 
131                 /*find v1 and v2*/
132                 v1 = (BME_Vert*)efa->v1->tmp.v;
133                 v2 = (BME_Vert*)efa->v2->tmp.v;
134                 
135                 f = BME_MF(bm,v1,v2,edar,len);
136                 f->mat_nr = efa->mat_nr;
137                 f->flag = efa->flag;
138                 if(efa->h) {
139                         f->flag |= ME_HIDE;
140                         f->flag &= ~ME_FACE_SEL;
141                 }
142                 else {
143                         if(efa->f & 1) f->flag |= ME_FACE_SEL;
144                         else f->flag &= ~ME_FACE_SEL;
145                 }
146                 efa = efa->next;
147         }
148         BME_model_end(bm);
149         return bm;
150 }
151
152 /* adds the geometry in the bmesh to G.editMesh (does not free G.editMesh)
153  * if td != NULL, the transdata will be mapped to the EditVert's co */
154 EditMesh *BME_bmesh_to_editmesh(BME_Mesh *bm, BME_TransData_Head *td) {
155         BME_Vert *v1;
156         BME_Edge *e;
157         BME_Poly *f;
158         
159         BME_TransData *vtd;
160
161         EditMesh *em;
162         EditVert *eve1, *eve2, *eve3, *eve4, **evlist;
163         EditEdge *eed;
164         EditFace *efa;
165
166         int totvert, len, i;
167
168         em = G.editMesh;
169
170         if (em == NULL) return NULL;
171
172
173         CustomData_copy(&bm->vdata, &em->vdata, CD_MASK_BMESH, CD_CALLOC, 0);
174         /* convert to EditMesh */
175         /* make editverts */
176         totvert = BLI_countlist(&(bm->verts));
177         evlist= (EditVert **)MEM_mallocN(totvert*sizeof(void *),"evlist");
178         for (i=0,v1=bm->verts.first;v1;v1=v1->next,i++) {
179                 v1->tflag1 = i;
180                 eve1 = addvertlist(v1->co,NULL);
181                 if (td && (vtd = BME_get_transdata(td,v1))) {
182                         vtd->loc = eve1->co;
183                 }
184                 eve1->keyindex = i;
185                 evlist[i]= eve1;
186                 eve1->f = (unsigned char)v1->flag;
187                 eve1->h = (unsigned char)v1->h;
188                 eve1->bweight = v1->bweight;
189                 CustomData_em_copy_data(&bm->vdata, &em->vdata, v1->data, &eve1->data);
190         }
191         
192         /* make edges */
193         for (e=bm->edges.first;e;e=e->next) {
194                 if(!(findedgelist(evlist[e->v1->tflag1], evlist[e->v2->tflag1]))){
195                         eed= addedgelist(evlist[e->v1->tflag1], evlist[e->v2->tflag1], NULL);
196                         eed->crease = e->crease;
197                         eed->bweight = e->bweight;
198                         if(e->flag & ME_SEAM) eed->seam = 1;
199                         if(e->flag & ME_SHARP) eed->sharp = 1;
200                         if(e->flag & SELECT) eed->f |= SELECT;
201                         if(e->flag & ME_FGON) eed->h= EM_FGON; // 2 different defines!
202                         if(e->flag & ME_HIDE) eed->h |= 1;
203                         if(G.scene->selectmode==SCE_SELECT_EDGE) 
204                                 EM_select_edge(eed, eed->f & SELECT);
205                 }
206         }
207
208         /* make faces */
209         for (f=bm->polys.first;f;f=f->next) {
210                 len = BME_cycle_length(f->loopbase);
211                 if (len==3 || len==4) {
212                         eve1= evlist[f->loopbase->v->tflag1];
213                         eve2= evlist[f->loopbase->next->v->tflag1];
214                         eve3= evlist[f->loopbase->next->next->v->tflag1];
215                         if (len == 4) {
216                                 eve4= evlist[f->loopbase->prev->v->tflag1];
217                         }
218                         else {
219                                 eve4= NULL;
220                         }
221
222                         efa = addfacelist(eve1, eve2, eve3, eve4, NULL, NULL);
223                         efa->mat_nr = (unsigned char)f->mat_nr;
224                         efa->flag= f->flag & ~ME_HIDE;
225                         if(f->flag & ME_FACE_SEL) {
226                                 efa->f |= SELECT;
227                         }
228                         if(f->flag & ME_HIDE) efa->h= 1;
229                         if((G.f & G_FACESELECT) && (efa->f & SELECT))
230                                 EM_select_face(efa, 1); /* flush down */
231                 }
232         }
233
234         MEM_freeN(evlist);
235
236         countall();
237
238         return em;
239 }
240
241 /* Adds the geometry found in dm to bm
242   */
243 BME_Mesh *BME_derivedmesh_to_bmesh(DerivedMesh *dm)
244 {
245         
246         BME_Mesh *bm;
247         int allocsize[4] = {512,512,2048,512};
248         MVert *mvert, *mv;
249         MEdge *medge, *me;
250         MFace *mface, *mf;
251         int totface,totedge,totvert,i,len;
252         BME_Vert *v1=NULL,*v2=NULL, **vert_array;
253         BME_Edge *e=NULL;
254         BME_Poly *f=NULL;
255         
256         EdgeHash *edge_hash = BLI_edgehash_new();
257
258         bm = BME_make_mesh(allocsize);
259         totvert = dm->getNumVerts(dm);
260         totedge = dm->getNumEdges(dm);
261         totface = dm->getNumFaces(dm);
262         mvert = dm->getVertArray(dm);
263         medge = dm->getEdgeArray(dm);
264         mface = dm->getFaceArray(dm);
265
266         vert_array = MEM_mallocN(sizeof(*vert_array)*totvert,"BME_derivedmesh_to_bmesh BME_Vert* array");
267
268         BME_model_begin(bm);
269         /*add verts*/
270         for(i=0,mv = mvert; i < totvert;i++,mv++){
271                 v1 = BME_MV(bm,mv->co);
272                 vert_array[i] = v1;
273                 v1->flag = mv->flag;
274                 v1->bweight = mv->bweight/255.0f;
275         }
276         /*add edges*/
277         for(i=0,me = medge; i < totedge;i++,me++){
278                 v1 = vert_array[me->v1];
279                 v2 = vert_array[me->v2];
280                 e = BME_ME(bm, v1, v2);
281                 e->crease = me->crease/255.0f;
282                 e->bweight = me->bweight/255.0f;
283                 e->flag = (unsigned char)me->flag;
284                 BLI_edgehash_insert(edge_hash,me->v1,me->v2,e);
285         }
286         /*add faces.*/
287         for(i=0,mf = mface; i < totface;i++,mf++){
288                 BME_Edge *edar[4];
289                 if(mf->v4) len = 4;
290                 else len = 3;
291                 
292                 edar[0] = BLI_edgehash_lookup(edge_hash,mf->v1,mf->v2);
293                 edar[1] = BLI_edgehash_lookup(edge_hash,mf->v2,mf->v3);
294                 if(len == 4){
295                         edar[2] = BLI_edgehash_lookup(edge_hash,mf->v3,mf->v4);
296                         edar[3] = BLI_edgehash_lookup(edge_hash,mf->v4,mf->v1);
297                 }
298                 else
299                         edar[2] = BLI_edgehash_lookup(edge_hash,mf->v3,mf->v1);
300                 
301                 /*find v1 and v2*/
302                 v1 = vert_array[mf->v1];
303                 v2 = vert_array[mf->v2];
304                 
305                 f = BME_MF(bm,v1,v2,edar,len);
306                 f->mat_nr = mf->mat_nr;
307                 f->flag = mf->flag;
308         }
309         
310         BME_model_end(bm);
311         BLI_edgehash_free(edge_hash, NULL);
312         MEM_freeN(vert_array);
313         return bm;
314 }
315
316 DerivedMesh *BME_bmesh_to_derivedmesh(BME_Mesh *bm, DerivedMesh *dm)
317 {
318         MFace *mface, *mf;
319         MEdge *medge, *me;
320         MVert *mvert, *mv;
321         int totface,totedge,totvert,i,bmeshok,len;
322
323         BME_Vert *v1=NULL;
324         BME_Edge *e=NULL, *oe=NULL;
325         BME_Poly *f=NULL;
326         
327         DerivedMesh *result;
328         EdgeHash *edge_hash = BLI_edgehash_new();
329
330         totvert = BLI_countlist(&(bm->verts));
331         totedge = 0;
332         
333         /*we cannot have double edges in a derived mesh!*/
334         for(i=0, v1=bm->verts.first; v1; v1=v1->next, i++) v1->tflag1 = i;
335         for(e=bm->edges.first; e; e=e->next){
336                 oe = BLI_edgehash_lookup(edge_hash,e->v1->tflag1, e->v2->tflag1);
337                 if(!oe){
338                         totedge++;
339                         BLI_edgehash_insert(edge_hash,e->v1->tflag1,e->v2->tflag1,e);
340                         e->tflag2 = 1;
341                 }
342                 else{
343                         e->tflag2 = 0;
344                 }
345         }
346         
347         /*count quads and tris*/
348         totface = 0;
349         bmeshok = 1;
350         for(f=bm->polys.first;f;f=f->next){
351                 len = BME_cycle_length(f->loopbase);
352                 if(len == 3 || len == 4) totface++;
353         }
354         
355         /*convert back to mesh*/
356         result = CDDM_from_template(dm,totvert,totedge,totface);
357         /*Make Verts*/
358         mvert = CDDM_get_verts(result);
359         for(i=0,v1=bm->verts.first,mv=mvert;v1;v1=v1->next,i++,mv++){
360                 VECCOPY(mv->co,v1->co);
361                 mv->flag = (unsigned char)v1->flag;
362                 mv->bweight = (char)(255.0*v1->bweight);
363         }
364         medge = CDDM_get_edges(result);
365         i=0;
366         for(e=bm->edges.first,me=medge;e;e=e->next){
367                 if(e->tflag2){
368                         if(e->v1->tflag1 < e->v2->tflag1){
369                                 me->v1 = e->v1->tflag1;
370                                 me->v2 = e->v2->tflag1;
371                         }
372                         else{
373                                 me->v1 = e->v2->tflag1;
374                                 me->v2 = e->v1->tflag1;
375                         }
376                 
377                         me->crease = (char)(255.0*e->crease);
378                         me->bweight = (char)(255.0*e->bweight);
379                         me->flag = e->flag;
380                         me++;
381                         i++;
382                 }
383         }
384         if(totface){
385                 mface = CDDM_get_faces(result);
386                 /*make faces*/
387                 for(i=0,f=bm->polys.first;f;f=f->next){
388                         mf = &mface[i];
389                         len = BME_cycle_length(f->loopbase);
390                         if(len==3 || len==4){
391                                 mf->v1 = f->loopbase->v->tflag1;
392                                 mf->v2 = f->loopbase->next->v->tflag1;
393                                 mf->v3 = f->loopbase->next->next->v->tflag1;
394                                 if(len == 4){
395                                         mf->v4 = f->loopbase->prev->v->tflag1;
396                                 }
397                                 /* test and rotate indexes if necessary so that verts 3 and 4 aren't index 0 */
398                                 if(mf->v3 == 0 || (len == 4 && mf->v4 == 0)){
399                                         test_index_face(mf, NULL, i, len);
400                                 }
401                                 i++;
402                                 mf->mat_nr = (unsigned char)f->mat_nr;
403                                 mf->flag = (unsigned char)f->flag;
404                         }
405                 }
406         }
407         BLI_edgehash_free(edge_hash, NULL);
408         return result;
409 }