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