5e8c295d7b806553fe662b1fe515c0cd3bcd7ae4
[blender-staging.git] / source / blender / editors / mesh / bmeshutils.c
1  /* $Id: bmeshutils.c
2  *
3  * ***** BEGIN GPL LICENSE BLOCK *****
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
18  *
19  * The Original Code is Copyright (C) 2004 by Blender Foundation.
20  * All rights reserved.
21  *
22  * The Original Code is: all of this file.
23  *
24  * Contributor(s): Joseph Eagar
25  *
26  * ***** END GPL LICENSE BLOCK *****
27  */
28 #include <stdlib.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <math.h>
32 #include <float.h>
33
34 #include "MEM_guardedalloc.h"
35 #include "PIL_time.h"
36
37 #include "BLO_sys_types.h" // for intptr_t support
38
39 #include "DNA_mesh_types.h"
40 #include "DNA_material_types.h"
41 #include "DNA_meshdata_types.h"
42 #include "DNA_modifier_types.h"
43 #include "DNA_object_types.h"
44 #include "DNA_scene_types.h"
45 #include "DNA_screen_types.h"
46 #include "DNA_view3d_types.h"
47 #include "DNA_key_types.h"
48 #include "DNA_windowmanager_types.h"
49
50 #include "RNA_types.h"
51 #include "RNA_define.h"
52 #include "RNA_access.h"
53
54 #include "BLI_blenlib.h"
55 #include "BLI_arithb.h"
56 #include "BLI_editVert.h"
57 #include "BLI_rand.h"
58 #include "BLI_ghash.h"
59 #include "BLI_linklist.h"
60 #include "BLI_heap.h"
61
62 #include "BKE_context.h"
63 #include "BKE_customdata.h"
64 #include "BKE_depsgraph.h"
65 #include "BKE_global.h"
66 #include "BKE_library.h"
67 #include "BKE_mesh.h"
68 #include "BKE_object.h"
69 #include "BKE_utildefines.h"
70 #include "BKE_bmesh.h"
71 #include "BKE_report.h"
72 #include "BKE_tessmesh.h"
73
74 #include "BIF_gl.h"
75 #include "BIF_glutil.h"
76
77 #include "WM_api.h"
78 #include "WM_types.h"
79
80 #include "ED_mesh.h"
81 #include "ED_view3d.h"
82 #include "ED_util.h"
83 #include "ED_screen.h"
84
85 #include "UI_interface.h"
86
87 #include "mesh_intern.h"
88 #include "bmesh.h"
89
90 void EDBM_RecalcNormals(BMEditMesh *em)
91 {
92         BM_Compute_Normals(em->bm);
93 }
94
95 void EDBM_stats_update(BMEditMesh *em)
96 {
97         BMIter iter;
98         BMHeader *ele;
99         int types[3] = {BM_VERTS_OF_MESH, BM_EDGES_OF_MESH, BM_FACES_OF_MESH};
100         int *tots[3];
101         int i;
102
103         tots[0] = &em->bm->totvertsel;
104         tots[1] = &em->bm->totedgesel;
105         tots[2] = &em->bm->totfacesel;
106         
107         em->bm->totvertsel = em->bm->totedgesel = em->bm->totfacesel = 0;
108
109         for (i=0; i<3; i++) {
110                 ele = BMIter_New(&iter, em->bm, types[i], NULL);
111                 for ( ; ele; ele=BMIter_Step(&iter)) {
112                         if (BM_TestHFlag(ele, BM_SELECT)) {
113                                 *tots[i]++;
114                         }
115                 }
116         }
117 }
118
119 /*this function is defunct, dead*/
120 void EDBM_Tesselate(EditMesh *em)
121 {
122         EditMesh *em2;
123         EditFace *efa;
124         BMesh *bm;
125         int found=0;
126         
127         for (efa=em->faces.first; efa; efa=efa->next) {
128                 if ((efa->e1->h & EM_FGON) || (efa->e2->h & EM_FGON) ||
129                     (efa->e3->h & EM_FGON) || (efa->e4&&(efa->e4->h&EM_FGON)))
130                 {
131                         found = 1;
132                         break;
133                 }
134         }
135
136         if (found) {
137                 bm = editmesh_to_bmesh(em);
138                 em2 = bmesh_to_editmesh(bm);
139                 set_editMesh(em, em2);
140
141                 MEM_freeN(em2);
142                 BM_Free_Mesh(bm);
143         }
144 }
145
146 int EDBM_InitOpf(BMEditMesh *em, BMOperator *bmop, wmOperator *op, char *fmt, ...)
147 {
148         BMesh *bm = em->bm;
149         va_list list;
150
151         va_start(list, fmt);
152
153         if (!BMO_VInitOpf(bm, bmop, fmt, list)) {
154                 BKE_report(op->reports, RPT_ERROR,
155                            "Parse error in EDBM_CallOpf");
156                 va_end(list);
157                 return 0;
158         }
159
160         em->emcopy = BMEdit_Copy(em);
161
162         va_end(list);
163 }
164
165
166 /*returns 0 on error, 1 on success.  executes and finishes a bmesh operator*/
167 int EDBM_FinishOp(BMEditMesh *em, BMOperator *bmop, wmOperator *op, int report) {
168         char *errmsg;
169         
170         BMO_Finish_Op(em->bm, bmop);
171
172         if (BMO_GetError(em->bm, &errmsg, NULL)) {
173                 BMEditMesh *emcopy = em->emcopy;
174
175                 if (report) BKE_report(op->reports, RPT_ERROR, errmsg);
176
177                 BMEdit_Free(em);
178                 *em = *emcopy;
179
180                 MEM_freeN(emcopy);
181                 return 0;
182         } else {
183                 BMEdit_Free(em->emcopy);
184                 MEM_freeN(em->emcopy);
185                 em->emcopy = NULL;
186         }
187
188         return 1;
189 }
190
191 int EDBM_CallOpf(BMEditMesh *em, wmOperator *op, char *fmt, ...)
192 {
193         BMesh *bm = em->bm;
194         BMOperator bmop;
195         va_list list;
196
197         va_start(list, fmt);
198
199         if (!BMO_VInitOpf(bm, &bmop, fmt, list)) {
200                 BKE_report(op->reports, RPT_ERROR,
201                            "Parse error in EDBM_CallOpf");
202                 va_end(list);
203                 return 0;
204         }
205
206         em->emcopy = BMEdit_Copy(em);
207
208         BMO_Exec_Op(bm, &bmop);
209
210         va_end(list);
211         return EDBM_FinishOp(em, &bmop, op, 1);
212 }
213
214 int EDBM_CallOpfSilent(BMEditMesh *em, char *fmt, ...)
215 {
216         BMesh *bm = em->bm;
217         BMOperator bmop;
218         va_list list;
219
220         va_start(list, fmt);
221
222         if (!BMO_VInitOpf(bm, &bmop, fmt, list)) {
223                 va_end(list);
224                 return 0;
225         }
226
227         em->emcopy = BMEdit_Copy(em);
228
229         BMO_Exec_Op(bm, &bmop);
230
231         va_end(list);
232         return EDBM_FinishOp(em, &bmop, NULL, 0);
233 }
234
235 void EDBM_MakeEditBMesh(Scene *scene, Object *ob)
236 {
237         Mesh *me = ob->data;
238         EditMesh *em;
239         BMesh *bm;
240
241         if (!me->mpoly && me->totface) {
242                 em = make_editMesh(scene, ob);
243                 bm = editmesh_to_bmesh(em);
244         
245                 free_editMesh(em);
246         } else {
247                 bm = BKE_mesh_to_bmesh(me);
248         }
249
250         me->edit_btmesh = BMEdit_Create(bm);
251         me->edit_btmesh->selectmode = scene->toolsettings->selectmode;
252 }
253
254 void EDBM_LoadEditBMesh(Scene *scene, Object *ob)
255 {
256         Mesh *me = ob->data;
257         BMesh *bm = me->edit_btmesh->bm;
258
259         BMO_CallOpf(bm, "object_load_bmesh scene=%p object=%p", scene, ob);
260
261 #if 0
262         EditMesh *em = bmesh_to_editmesh(me->edit_btmesh->bm);
263         
264         load_editMesh(scene, ob, em);
265         free_editMesh(em);
266         MEM_freeN(em);
267 #endif
268 }
269
270 void EDBM_FreeEditBMesh(BMEditMesh *tm)
271 {
272         BMEdit_Free(tm);
273 }
274
275 void EDBM_init_index_arrays(BMEditMesh *tm, int forvert, int foredge, int forface)
276 {
277         EDBM_free_index_arrays(tm);
278
279         if (forvert) {
280                 BMIter iter;
281                 BMVert *ele;
282                 int i=0;
283                 
284                 tm->vert_index = MEM_mallocN(sizeof(void**)*tm->bm->totvert, "tm->vert_index");
285
286                 ele = BMIter_New(&iter, tm->bm, BM_VERTS_OF_MESH, NULL);
287                 for ( ; ele; ele=BMIter_Step(&iter)) {
288                         tm->vert_index[i++] = ele;
289                 }
290         }
291
292         if (foredge) {
293                 BMIter iter;
294                 BMEdge *ele;
295                 int i=0;
296                 
297                 tm->edge_index = MEM_mallocN(sizeof(void**)*tm->bm->totedge, "tm->edge_index");
298
299                 ele = BMIter_New(&iter, tm->bm, BM_EDGES_OF_MESH, NULL);
300                 for ( ; ele; ele=BMIter_Step(&iter)) {
301                         tm->edge_index[i++] = ele;
302                 }
303         }
304
305         if (forface) {
306                 BMIter iter;
307                 BMFace *ele;
308                 int i=0;
309                 
310                 tm->face_index = MEM_mallocN(sizeof(void**)*tm->bm->totface, "tm->face_index");
311
312                 ele = BMIter_New(&iter, tm->bm, BM_FACES_OF_MESH, NULL);
313                 for ( ; ele; ele=BMIter_Step(&iter)) {
314                         tm->face_index[i++] = ele;
315                 }
316         }
317 }
318
319 void EDBM_free_index_arrays(BMEditMesh *tm)
320 {
321         if (tm->vert_index) {
322                 MEM_freeN(tm->vert_index);
323                 tm->vert_index = NULL;
324         }
325
326         if (tm->edge_index) {
327                 MEM_freeN(tm->edge_index);
328                 tm->edge_index = NULL;
329         }
330
331         if (tm->face_index) {
332                 MEM_freeN(tm->face_index);
333                 tm->face_index = NULL;
334         }
335 }
336
337 BMVert *EDBM_get_vert_for_index(BMEditMesh *tm, int index)
338 {
339         return tm->vert_index?tm->vert_index[index]:NULL;
340 }
341
342 BMEdge *EDBM_get_edge_for_index(BMEditMesh *tm, int index)
343 {
344         return tm->edge_index?tm->edge_index[index]:NULL;
345 }
346
347 BMFace *EDBM_get_face_for_index(BMEditMesh *tm, int index)
348 {
349         return tm->face_index?tm->face_index[index]:NULL;
350 }
351
352 /* this replaces the active flag used in uv/face mode */
353 void EDBM_set_actFace(BMEditMesh *em, BMFace *efa)
354 {
355         em->bm->act_face = efa;
356 }
357
358 BMFace *EDBM_get_actFace(BMEditMesh *em, int sloppy)
359 {
360         if (em->bm->act_face) {
361                 return em->bm->act_face;
362         } else if (sloppy) {
363                 BMFace *efa= NULL;
364                 BMEditSelection *ese;
365                 
366                 ese = em->bm->selected.last;
367                 for (; ese; ese=ese->prev){
368                         if(ese->type == BM_FACE) {
369                                 efa = (BMFace *)ese->data;
370                                 
371                                 if (BM_TestHFlag(efa, BM_HIDDEN)) efa= NULL;
372                                 else break;
373                         }
374                 }
375                 if (efa==NULL) {
376                         BMIter iter;
377                         efa = BMIter_New(&iter, em->bm, BM_FACES_OF_MESH, NULL);
378                         for ( ; efa; efa=BMIter_Step(&iter)) {
379                                 if (BM_TestHFlag(efa, BM_SELECT))
380                                         break;
381                         }
382                 }
383                 return efa; /* can still be null */
384         }
385         return NULL;
386
387 }
388
389 void EDBM_selectmode_flush(BMEditMesh *em)
390 {
391         em->bm->selectmode = em->selectmode;
392         BM_SelectMode_Flush(em->bm);
393 }
394
395
396 int EDBM_get_actSelection(BMEditMesh *em, BMEditSelection *ese)
397 {
398         BMEditSelection *ese_last = em->bm->selected.last;
399         BMFace *efa = EDBM_get_actFace(em, 0);
400
401         ese->next = ese->prev = NULL;
402         
403         if (ese_last) {
404                 if (ese_last->type == BM_FACE) { /* if there is an active face, use it over the last selected face */
405                         if (efa) {
406                                 ese->data = (void *)efa;
407                         } else {
408                                 ese->data = ese_last->data;
409                         }
410                         ese->type = BM_FACE;
411                 } else {
412                         ese->data = ese_last->data;
413                         ese->type = ese_last->type;
414                 }
415         } else if (efa) { /* no */
416                 ese->data = (void *)efa;
417                 ese->type = BM_FACE;
418         } else {
419                 ese->data = NULL;
420                 return 0;
421         }
422         return 1;
423 }
424
425 void EDBM_clear_flag_all(BMEditMesh *em, int flag)
426 {
427         BMIter iter;
428         BMHeader *ele;
429         int i, type;
430
431         for (i=0; i<3; i++) {
432                 switch (i) {
433                         case 0:
434                                 type = BM_VERTS_OF_MESH;
435                                 break;
436                         case 1:
437                                 type = BM_EDGES_OF_MESH;
438                                 break;
439                         case 2:
440                                 type = BM_FACES_OF_MESH;
441                                 break;
442                 }
443                 ele = BMIter_New(&iter, em->bm, type, NULL);
444                 for ( ; ele; ele=BMIter_Step(&iter)) {
445                         if (flag & BM_SELECT) BM_Select(em->bm, ele, 0);
446                         BM_ClearHFlag(ele, flag);
447                 }
448         }
449 }
450
451
452 void EDBM_set_flag_all(BMEditMesh *em, int flag)
453 {
454         BMIter iter;
455         BMHeader *ele;
456         int i, type;
457
458         for (i=0; i<3; i++) {
459                 switch (i) {
460                         case 0:
461                                 type = BM_VERTS_OF_MESH;
462                                 break;
463                         case 1:
464                                 type = BM_EDGES_OF_MESH;
465                                 break;
466                         case 2:
467                                 type = BM_FACES_OF_MESH;
468                                 break;
469                 }
470                 ele = BMIter_New(&iter, em->bm, type, NULL);
471                 for ( ; ele; ele=BMIter_Step(&iter)) {
472                         if (flag & BM_SELECT) BM_Select(em->bm, ele, 1);
473                         BM_SetHFlag(ele, flag);
474                 }
475         }
476 }
477
478 /**************-------------- Undo ------------*****************/
479
480 /* for callbacks */
481
482 static void *getEditMesh(bContext *C)
483 {
484         Object *obedit= CTX_data_edit_object(C);
485         if(obedit && obedit->type==OB_MESH) {
486                 Mesh *me= obedit->data;
487                 return me->edit_btmesh;
488         }
489         return NULL;
490 }
491
492 /*undo simply makes copies of a bmesh*/
493 static void *editbtMesh_to_undoMesh(void *emv)
494 {
495         /*we recalc the tesselation here, to avoid seeding calls to
496           BMEdit_RecalcTesselation throughout the code.*/
497         BMEdit_RecalcTesselation(emv);
498
499         return BMEdit_Copy(emv);
500 }
501
502 static void undoMesh_to_editbtMesh(void *umv, void *emv)
503 {
504         BMEditMesh *bm1 = umv, *bm2 = emv;
505
506         BMEdit_Free(bm2);
507
508         *bm2 = *BMEdit_Copy(bm1);
509 }
510
511
512 static void free_undo(void *umv)
513 {
514         BMEditMesh *em = umv;
515
516         BMEdit_Free(em);
517 }
518
519 /* and this is all the undo system needs to know */
520 void undo_push_mesh(bContext *C, char *name)
521 {
522         undo_editmode_push(C, name, getEditMesh, free_undo, undoMesh_to_editbtMesh, editbtMesh_to_undoMesh, NULL);
523 }