=bmesh= edge split modifier uses bmesh code for now, slower but works.
[blender.git] / source / blender / modifiers / intern / MOD_edgesplit.c
1 /*
2 * $Id$
3 *
4 * ***** BEGIN GPL LICENSE BLOCK *****
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software  Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 *
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
22 *
23 * Contributor(s): Joseph Eagar
24 *
25 * ***** END GPL LICENSE BLOCK *****
26 *
27 */
28
29 /* EdgeSplit modifier: Splits edges in the mesh according to sharpness flag
30  * or edge angle (can be used to achieve autosmoothing) */
31
32 #include "DNA_meshdata_types.h"
33
34 #include "BLI_utildefines.h"
35 #include "BLI_listbase.h"
36 #include "BLI_memarena.h"
37 #include "BLI_edgehash.h"
38 #include "BLI_math.h"
39 #include "BLI_array.h"
40 #include "BLI_smallhash.h"
41
42 #include "BKE_cdderivedmesh.h"
43 #include "BKE_modifier.h"
44 #include "BKE_particle.h"
45 #include "BKE_tessmesh.h"
46 #include "BKE_mesh.h"
47
48 #include "MEM_guardedalloc.h"
49
50 /* EdgeSplit */
51 /* EdgeSplit modifier: Splits edges in the mesh according to sharpness flag
52  * or edge angle (can be used to achieve autosmoothing)
53 */
54
55 #define EDGE_MARK       1
56
57 DerivedMesh *doEdgeSplit(DerivedMesh *dm, EdgeSplitModifierData *emd, Object *ob)
58 {
59         BMesh *bm;
60         BMEditMesh *em;
61         DerivedMesh *cddm, *bmdm;
62         BMIter iter, liter;
63         BMFace *f;
64         BMLoop *l;
65         BMEdge *e;
66         int allocsize[] = {512, 512, 2048, 512};
67         float threshold = cos((emd->split_angle + 0.00001) * M_PI / 180.0);
68         
69         if (!CDDM_Check(dm)) {
70                 cddm = CDDM_copy(dm, 0);
71         } else cddm = dm;
72         
73         em = CDDM_To_BMesh(ob, dm, NULL);
74         bm = em->bm;
75         
76         BMO_push(bm, NULL);
77         
78         if (emd->flags & MOD_EDGESPLIT_FROMANGLE) {
79                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
80                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, l) {
81                                 float edge_angle_cos;
82                                 
83                                 if (l->radial_next == l)
84                                         continue;
85                                 
86                                 edge_angle_cos = INPR(f->no, l->radial_next->f->no);
87                                 if (edge_angle_cos < threshold) {
88                                         BMO_SetFlag(bm, l->e, EDGE_MARK);
89                                 }
90                         }
91                 }
92         }
93         
94         BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
95                 if (BM_TestHFlag(e, BM_SHARP))
96                         BMO_SetFlag(bm, e, EDGE_MARK);
97         }
98         
99         BMO_CallOpf(bm, "edgesplit edges=%fe", EDGE_MARK);
100         
101         BMO_pop(bm);
102         BMEdit_RecalcTesselation(em);
103         
104         if (cddm != dm) {
105                 cddm->needsFree = 1;
106                 cddm->release(cddm);
107         }
108         
109         cddm = CDDM_from_BMEditMesh(em, NULL, 1);
110         BMEdit_Free(em);
111         MEM_freeN(em);
112         
113         return cddm;
114 }
115
116 static void initData(ModifierData *md)
117 {
118         EdgeSplitModifierData *emd = (EdgeSplitModifierData*) md;
119
120         /* default to 30-degree split angle, sharpness from both angle & flag
121         */
122         emd->split_angle = 30;
123         emd->flags = MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG;
124 }
125
126 static void copyData(ModifierData *md, ModifierData *target)
127 {
128         EdgeSplitModifierData *emd = (EdgeSplitModifierData*) md;
129         EdgeSplitModifierData *temd = (EdgeSplitModifierData*) target;
130
131         temd->split_angle = emd->split_angle;
132         temd->flags = emd->flags;
133 }
134
135 static DerivedMesh *edgesplitModifier_do(EdgeSplitModifierData *emd,
136                                          Object *ob, DerivedMesh *dm)
137 {
138         if(!(emd->flags & (MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG)))
139                 return dm;
140
141         return doEdgeSplit(dm, emd, ob);
142 }
143
144 static DerivedMesh *applyModifier(
145                 ModifierData *md, Object *ob, DerivedMesh *derivedData,
146                 int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
147 {
148         DerivedMesh *result;
149         EdgeSplitModifierData *emd = (EdgeSplitModifierData*) md;
150
151         result = edgesplitModifier_do(emd, ob, derivedData);
152
153         if(result != derivedData)
154                 CDDM_calc_normals(result);
155
156         return result;
157 }
158
159 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
160                                                                         BMEditMesh *UNUSED(editData), DerivedMesh *derivedData)
161 {
162         return applyModifier(md, ob, derivedData, 0, 1);
163 }
164
165
166 ModifierTypeInfo modifierType_EdgeSplit = {
167         /* name */              "EdgeSplit",
168         /* structName */        "EdgeSplitModifierData",
169         /* structSize */        sizeof(EdgeSplitModifierData),
170         /* type */              eModifierTypeType_Constructive,
171         /* flags */             eModifierTypeFlag_AcceptsMesh
172                                                         | eModifierTypeFlag_AcceptsCVs
173                                                         | eModifierTypeFlag_SupportsMapping
174                                                         | eModifierTypeFlag_SupportsEditmode
175                                                         | eModifierTypeFlag_EnableInEditmode,
176
177         /* copyData */          copyData,
178         /* deformVerts */       NULL,
179         /* deformMatrices */    NULL,
180         /* deformVertsEM */     NULL,
181         /* deformMatricesEM */  NULL,
182         /* applyModifier */     applyModifier,
183         /* applyModifierEM */   applyModifierEM,
184         /* initData */          initData,
185         /* requiredDataMask */  NULL,
186         /* freeData */          NULL,
187         /* isDisabled */        NULL,
188         /* updateDepgraph */    NULL,
189         /* dependsOnTime */     NULL,
190         /* dependsOnNormals */  NULL,
191         /* foreachObjectLink */ NULL,
192         /* foreachIDLink */     NULL,
193 };