d877932b81de9fb34b27a44aa1862f0bc722b64a
[blender.git] / source / blender / modifiers / intern / MOD_edgesplit.c
1 /*
2  * ***** BEGIN GPL LICENSE BLOCK *****
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software  Foundation,
16  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17  *
18  * The Original Code is Copyright (C) 2005 by the Blender Foundation.
19  * All rights reserved.
20  *
21  * Contributor(s): Daniel Dunbar
22  *                 Ton Roosendaal,
23  *                 Ben Batt,
24  *                 Brecht Van Lommel,
25  *                 Campbell Barton
26  *
27  * ***** END GPL LICENSE BLOCK *****
28  *
29  */
30
31 /** \file blender/modifiers/intern/MOD_edgesplit.c
32  *  \ingroup modifiers
33  *
34  * EdgeSplit modifier
35  *
36  * Splits edges in the mesh according to sharpness flag
37  * or edge angle (can be used to achieve autosmoothing)
38  */
39
40 #include "BLI_utildefines.h"
41 #include "BLI_math.h"
42
43 #include "BKE_cdderivedmesh.h"
44 #include "BKE_modifier.h"
45
46 #include "bmesh.h"
47 #include "tools/bmesh_edgesplit.h"
48
49 #include "DNA_object_types.h"
50
51
52 static DerivedMesh *doEdgeSplit(DerivedMesh *dm, EdgeSplitModifierData *emd, Object *UNUSED(ob))
53 {
54         DerivedMesh *result;
55         BMesh *bm;
56         BMIter iter;
57         BMEdge *e;
58         float threshold = cosf((emd->split_angle + 0.00001f) * (float)M_PI / 180.0f);
59         const bool calc_face_normals = (emd->flags & MOD_EDGESPLIT_FROMANGLE) != 0;
60
61         bm = DM_to_bmesh(dm, calc_face_normals);
62         
63         if (emd->flags & MOD_EDGESPLIT_FROMANGLE) {
64                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
65                         /* check for 1 edge having 2 face users */
66                         BMLoop *l1, *l2;
67                         if ((l1 = e->l) &&
68                             (l2 = e->l->radial_next) != l1)
69                         {
70                                 if (/* 3+ faces on this edge, always split */
71                                     UNLIKELY(l1 != l2->radial_next) ||
72                                     /* 2 face edge - check angle*/
73                                     (dot_v3v3(l1->f->no, l2->f->no) < threshold))
74                                 {
75                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
76                                 }
77                         }
78                 }
79         }
80         
81         if (emd->flags & MOD_EDGESPLIT_FROMFLAG) {
82                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
83                         /* check for 2 or more edge users */
84                         if ((e->l) &&
85                             (e->l->next != e->l))
86                         {
87                                 if (!BM_elem_flag_test(e, BM_ELEM_SMOOTH)) {
88                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
89                                 }
90                         }
91                 }
92         }
93         
94         BM_mesh_edgesplit(bm, FALSE, TRUE);
95
96         /* BM_mesh_validate(bm); */ /* for troubleshooting */
97
98         result = CDDM_from_bmesh(bm, TRUE);
99         BM_mesh_free(bm);
100         
101         return result;
102 }
103
104 static void initData(ModifierData *md)
105 {
106         EdgeSplitModifierData *emd = (EdgeSplitModifierData *) md;
107
108         /* default to 30-degree split angle, sharpness from both angle & flag */
109         emd->split_angle = 30;
110         emd->flags = MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG;
111 }
112
113 static void copyData(ModifierData *md, ModifierData *target)
114 {
115         EdgeSplitModifierData *emd = (EdgeSplitModifierData *) md;
116         EdgeSplitModifierData *temd = (EdgeSplitModifierData *) target;
117
118         temd->split_angle = emd->split_angle;
119         temd->flags = emd->flags;
120 }
121
122 static DerivedMesh *edgesplitModifier_do(EdgeSplitModifierData *emd,
123                                          Object *ob, DerivedMesh *dm)
124 {
125         if (!(emd->flags & (MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG)))
126                 return dm;
127
128         return doEdgeSplit(dm, emd, ob);
129 }
130
131 static DerivedMesh *applyModifier(ModifierData *md, Object *ob, DerivedMesh *derivedData,
132                                   ModifierApplyFlag UNUSED(flag))
133 {
134         DerivedMesh *result;
135         EdgeSplitModifierData *emd = (EdgeSplitModifierData *) md;
136
137         result = edgesplitModifier_do(emd, ob, derivedData);
138
139         if (result != derivedData)
140                 result->dirty |= DM_DIRTY_NORMALS;
141
142         return result;
143 }
144
145
146 ModifierTypeInfo modifierType_EdgeSplit = {
147         /* name */              "EdgeSplit",
148         /* structName */        "EdgeSplitModifierData",
149         /* structSize */        sizeof(EdgeSplitModifierData),
150         /* type */              eModifierTypeType_Constructive,
151         /* flags */             eModifierTypeFlag_AcceptsMesh |
152                                 eModifierTypeFlag_AcceptsCVs |
153                                 eModifierTypeFlag_SupportsMapping |
154                                 eModifierTypeFlag_SupportsEditmode |
155                                 eModifierTypeFlag_EnableInEditmode,
156
157         /* copyData */          copyData,
158         /* deformVerts */       NULL,
159         /* deformMatrices */    NULL,
160         /* deformVertsEM */     NULL,
161         /* deformMatricesEM */  NULL,
162         /* applyModifier */     applyModifier,
163         /* applyModifierEM */   NULL,
164         /* initData */          initData,
165         /* requiredDataMask */  NULL,
166         /* freeData */          NULL,
167         /* isDisabled */        NULL,
168         /* updateDepgraph */    NULL,
169         /* dependsOnTime */     NULL,
170         /* dependsOnNormals */  NULL,
171         /* foreachObjectLink */ NULL,
172         /* foreachIDLink */     NULL,
173         /* foreachTexLink */    NULL,
174 };