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_id_new_nomain(ID_ME, mesh->id.name);
111         BM_mesh_bm_to_me(bm, result, &((struct BMeshToMeshParams){0}));
112         BM_mesh_free(bm);
113
114         result->runtime.cd_dirty_vert |= CD_MASK_NORMAL;
115         return result;
116 }
117
118 static void initData(ModifierData *md)
119 {
120         EdgeSplitModifierData *emd = (EdgeSplitModifierData *) md;
121
122         /* default to 30-degree split angle, sharpness from both angle & flag */
123         emd->split_angle = DEG2RADF(30.0f);
124         emd->flags = MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG;
125 }
126
127 static Mesh *applyModifier(
128         ModifierData *md,
129         const ModifierEvalContext *ctx,
130         Mesh *mesh)
131 {
132         Mesh *result;
133         EdgeSplitModifierData *emd = (EdgeSplitModifierData *) md;
134
135         if (!(emd->flags & (MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG)))
136                 return mesh;
137
138         result = doEdgeSplit(mesh, emd, ctx);
139
140         return result;
141 }
142
143
144 ModifierTypeInfo modifierType_EdgeSplit = {
145         /* name */              "EdgeSplit",
146         /* structName */        "EdgeSplitModifierData",
147         /* structSize */        sizeof(EdgeSplitModifierData),
148         /* type */              eModifierTypeType_Constructive,
149         /* flags */             eModifierTypeFlag_AcceptsMesh |
150                                 eModifierTypeFlag_AcceptsCVs |
151                                 eModifierTypeFlag_SupportsMapping |
152                                 eModifierTypeFlag_SupportsEditmode |
153                                 eModifierTypeFlag_EnableInEditmode,
154
155         /* copyData */          modifier_copyData_generic,
156
157         /* deformVerts_DM */    NULL,
158         /* deformMatrices_DM */ NULL,
159         /* deformVertsEM_DM */  NULL,
160         /* deformMatricesEM_DM*/NULL,
161         /* applyModifier_DM */  NULL,
162         /* applyModifierEM_DM */NULL,
163
164         /* deformVerts */       NULL,
165         /* deformMatrices */    NULL,
166         /* deformVertsEM */     NULL,
167         /* deformMatricesEM */  NULL,
168         /* applyModifier */     applyModifier,
169         /* applyModifierEM */   NULL,
170
171         /* initData */          initData,
172         /* requiredDataMask */  NULL,
173         /* freeData */          NULL,
174         /* isDisabled */        NULL,
175         /* updateDepsgraph */   NULL,
176         /* dependsOnTime */     NULL,
177         /* dependsOnNormals */  NULL,
178         /* foreachObjectLink */ NULL,
179         /* foreachIDLink */     NULL,
180         /* foreachTexLink */    NULL,
181 };