Merge branch 'master' into blender2.8
[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_library.h"
45 #include "BKE_mesh.h"
46 #include "BKE_modifier.h"
47
48 #include "bmesh.h"
49 #include "bmesh_tools.h"
50
51 #include "DNA_mesh_types.h"
52 #include "DNA_object_types.h"
53
54 #include "MOD_modifiertypes.h"
55
56 static Mesh *doEdgeSplit(Mesh *mesh, EdgeSplitModifierData *emd, const ModifierEvalContext *ctx)
57 {
58         Mesh *result;
59         BMesh *bm;
60         BMIter iter;
61         BMEdge *e;
62         float threshold = cosf(emd->split_angle + 0.000000175f);
63         const bool calc_face_normals = (emd->flags & MOD_EDGESPLIT_FROMANGLE) != 0;
64
65         bm = BKE_mesh_to_bmesh_ex(
66                 mesh,
67                 &(struct BMeshCreateParams){0},
68                 &(struct BMeshFromMeshParams){
69                     .calc_face_normal = calc_face_normals,
70                     .add_key_index = false,
71                     .use_shapekey = true,
72                     .active_shapekey = ctx->object->shapenr,
73                 });
74
75         if (emd->flags & MOD_EDGESPLIT_FROMANGLE) {
76                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
77                         /* check for 1 edge having 2 face users */
78                         BMLoop *l1, *l2;
79                         if ((l1 = e->l) &&
80                             (l2 = e->l->radial_next) != l1)
81                         {
82                                 if (/* 3+ faces on this edge, always split */
83                                     UNLIKELY(l1 != l2->radial_next) ||
84                                     /* 2 face edge - check angle*/
85                                     (dot_v3v3(l1->f->no, l2->f->no) < threshold))
86                                 {
87                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
88                                 }
89                         }
90                 }
91         }
92
93         if (emd->flags & MOD_EDGESPLIT_FROMFLAG) {
94                 BM_ITER_MESH (e, &iter, bm, BM_EDGES_OF_MESH) {
95                         /* check for 2 or more edge users */
96                         if ((e->l) &&
97                             (e->l->next != e->l))
98                         {
99                                 if (!BM_elem_flag_test(e, BM_ELEM_SMOOTH)) {
100                                         BM_elem_flag_enable(e, BM_ELEM_TAG);
101                                 }
102                         }
103                 }
104         }
105
106         BM_mesh_edgesplit(bm, false, true, false);
107
108         /* BM_mesh_validate(bm); */ /* for troubleshooting */
109
110         result = BKE_bmesh_to_mesh_nomain(bm, &((struct BMeshToMeshParams){0}));
111         BM_mesh_free(bm);
112
113         result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
114         return result;
115 }
116
117 static void initData(ModifierData *md)
118 {
119         EdgeSplitModifierData *emd = (EdgeSplitModifierData *) md;
120
121         /* default to 30-degree split angle, sharpness from both angle & flag */
122         emd->split_angle = DEG2RADF(30.0f);
123         emd->flags = MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG;
124 }
125
126 static Mesh *applyModifier(
127         ModifierData *md,
128         const ModifierEvalContext *ctx,
129         Mesh *mesh)
130 {
131         Mesh *result;
132         EdgeSplitModifierData *emd = (EdgeSplitModifierData *) md;
133
134         if (!(emd->flags & (MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG)))
135                 return mesh;
136
137         result = doEdgeSplit(mesh, emd, ctx);
138
139         return result;
140 }
141
142
143 ModifierTypeInfo modifierType_EdgeSplit = {
144         /* name */              "EdgeSplit",
145         /* structName */        "EdgeSplitModifierData",
146         /* structSize */        sizeof(EdgeSplitModifierData),
147         /* type */              eModifierTypeType_Constructive,
148         /* flags */             eModifierTypeFlag_AcceptsMesh |
149                                 eModifierTypeFlag_AcceptsCVs |
150                                 eModifierTypeFlag_SupportsMapping |
151                                 eModifierTypeFlag_SupportsEditmode |
152                                 eModifierTypeFlag_EnableInEditmode,
153
154         /* copyData */          modifier_copyData_generic,
155
156         /* deformVerts_DM */    NULL,
157         /* deformMatrices_DM */ NULL,
158         /* deformVertsEM_DM */  NULL,
159         /* deformMatricesEM_DM*/NULL,
160         /* applyModifier_DM */  NULL,
161         /* applyModifierEM_DM */NULL,
162
163         /* deformVerts */       NULL,
164         /* deformMatrices */    NULL,
165         /* deformVertsEM */     NULL,
166         /* deformMatricesEM */  NULL,
167         /* applyModifier */     applyModifier,
168         /* applyModifierEM */   NULL,
169
170         /* initData */          initData,
171         /* requiredDataMask */  NULL,
172         /* freeData */          NULL,
173         /* isDisabled */        NULL,
174         /* updateDepsgraph */   NULL,
175         /* dependsOnTime */     NULL,
176         /* dependsOnNormals */  NULL,
177         /* foreachObjectLink */ NULL,
178         /* foreachIDLink */     NULL,
179         /* foreachTexLink */    NULL,
180 };