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