svn merge -r37900:38000 https://svn.blender.org/svnroot/bf-blender/trunk/blender
[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;
62         BMIter iter, liter;
63         BMFace *f;
64         BMLoop *l;
65         BMEdge *e;
66         /* int allocsize[] = {512, 512, 2048, 512}; */ /* UNUSED */
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         BM_Compute_Normals(bm); 
77         BMO_push(bm, NULL);
78         
79         if (emd->flags & MOD_EDGESPLIT_FROMANGLE) {
80                 BM_ITER(f, &iter, bm, BM_FACES_OF_MESH, NULL) {
81                         BM_ITER(l, &liter, bm, BM_LOOPS_OF_FACE, f) {
82                                 float edge_angle_cos;
83                                 
84                                 if (l->radial_next == l)
85                                         continue;
86                                 
87                                 edge_angle_cos = INPR(f->no, l->radial_next->f->no);
88                                 if (edge_angle_cos < threshold) {
89                                         BMO_SetFlag(bm, l->e, EDGE_MARK);
90                                 }
91                         }
92                 }
93         }
94         
95         if (emd->flags & MOD_EDGESPLIT_FROMFLAG) {
96                 BM_ITER(e, &iter, bm, BM_EDGES_OF_MESH, NULL) {
97                         if (BM_TestHFlag(e, BM_SHARP))
98                                 BMO_SetFlag(bm, e, EDGE_MARK);
99                 }
100         }
101         
102         BMO_CallOpf(bm, "edgesplit edges=%fe", EDGE_MARK);
103         
104         BMO_pop(bm);
105         BMEdit_RecalcTesselation(em);
106         
107         if (cddm != dm) {
108                 cddm->needsFree = 1;
109                 cddm->release(cddm);
110         }
111         
112         cddm = CDDM_from_BMEditMesh(em, NULL, 1);
113         BMEdit_Free(em);
114         MEM_freeN(em);
115         
116         return cddm;
117 }
118
119 static void initData(ModifierData *md)
120 {
121         EdgeSplitModifierData *emd = (EdgeSplitModifierData*) md;
122
123         /* default to 30-degree split angle, sharpness from both angle & flag
124         */
125         emd->split_angle = 30;
126         emd->flags = MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG;
127 }
128
129 static void copyData(ModifierData *md, ModifierData *target)
130 {
131         EdgeSplitModifierData *emd = (EdgeSplitModifierData*) md;
132         EdgeSplitModifierData *temd = (EdgeSplitModifierData*) target;
133
134         temd->split_angle = emd->split_angle;
135         temd->flags = emd->flags;
136 }
137
138 static DerivedMesh *edgesplitModifier_do(EdgeSplitModifierData *emd,
139                                          Object *ob, DerivedMesh *dm)
140 {
141         if(!(emd->flags & (MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG)))
142                 return dm;
143
144         return doEdgeSplit(dm, emd, ob);
145 }
146
147 static DerivedMesh *applyModifier(
148                 ModifierData *md, Object *ob, DerivedMesh *derivedData,
149                 int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
150 {
151         DerivedMesh *result;
152         EdgeSplitModifierData *emd = (EdgeSplitModifierData*) md;
153
154         result = edgesplitModifier_do(emd, ob, derivedData);
155
156         if(result != derivedData)
157                 CDDM_calc_normals(result);
158
159         return result;
160 }
161
162 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
163                                                                         BMEditMesh *UNUSED(editData), DerivedMesh *derivedData)
164 {
165         return applyModifier(md, ob, derivedData, 0, 1);
166 }
167
168
169 ModifierTypeInfo modifierType_EdgeSplit = {
170         /* name */              "EdgeSplit",
171         /* structName */        "EdgeSplitModifierData",
172         /* structSize */        sizeof(EdgeSplitModifierData),
173         /* type */              eModifierTypeType_Constructive,
174         /* flags */             eModifierTypeFlag_AcceptsMesh
175                                                         | eModifierTypeFlag_AcceptsCVs
176                                                         | eModifierTypeFlag_SupportsMapping
177                                                         | eModifierTypeFlag_SupportsEditmode
178                                                         | eModifierTypeFlag_EnableInEditmode,
179
180         /* copyData */          copyData,
181         /* deformVerts */       NULL,
182         /* deformMatrices */    NULL,
183         /* deformVertsEM */     NULL,
184         /* deformMatricesEM */  NULL,
185         /* applyModifier */     applyModifier,
186         /* applyModifierEM */   applyModifierEM,
187         /* initData */          initData,
188         /* requiredDataMask */  NULL,
189         /* freeData */          NULL,
190         /* isDisabled */        NULL,
191         /* updateDepgraph */    NULL,
192         /* dependsOnTime */     NULL,
193         /* dependsOnNormals */  NULL,
194         /* foreachObjectLink */ NULL,
195         /* foreachIDLink */     NULL,
196 };