4 * ***** BEGIN GPL LICENSE BLOCK *****
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.
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.
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.
20 * The Original Code is Copyright (C) 2005 by the Blender Foundation.
21 * All rights reserved.
23 * Contributor(s): Joseph Eagar
25 * ***** END GPL LICENSE BLOCK *****
29 /* EdgeSplit modifier: Splits edges in the mesh according to sharpness flag
30 * or edge angle (can be used to achieve autosmoothing) */
32 #include "DNA_meshdata_types.h"
34 #include "BLI_utildefines.h"
35 #include "BLI_listbase.h"
36 #include "BLI_memarena.h"
37 #include "BLI_edgehash.h"
39 #include "BLI_array.h"
40 #include "BLI_smallhash.h"
42 #include "BKE_cdderivedmesh.h"
43 #include "BKE_modifier.h"
44 #include "BKE_particle.h"
45 #include "BKE_tessmesh.h"
48 #include "MEM_guardedalloc.h"
51 /* EdgeSplit modifier: Splits edges in the mesh according to sharpness flag
52 * or edge angle (can be used to achieve autosmoothing)
57 DerivedMesh *doEdgeSplit(DerivedMesh *dm, EdgeSplitModifierData *emd, Object *ob)
66 /* int allocsize[] = {512, 512, 2048, 512}; */ /* UNUSED */
67 float threshold = cos((emd->split_angle + 0.00001) * M_PI / 180.0);
69 if (!CDDM_Check(dm)) {
70 cddm = CDDM_copy(dm, 0);
73 em = CDDM_To_BMesh(ob, dm, NULL);
76 BM_Compute_Normals(bm);
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) {
84 if (l->radial_next == l)
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);
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);
102 BMO_CallOpf(bm, "edgesplit edges=%fe", EDGE_MARK);
105 BMEdit_RecalcTesselation(em);
112 cddm = CDDM_from_BMEditMesh(em, NULL, 1);
119 static void initData(ModifierData *md)
121 EdgeSplitModifierData *emd = (EdgeSplitModifierData*) md;
123 /* default to 30-degree split angle, sharpness from both angle & flag
125 emd->split_angle = 30;
126 emd->flags = MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG;
129 static void copyData(ModifierData *md, ModifierData *target)
131 EdgeSplitModifierData *emd = (EdgeSplitModifierData*) md;
132 EdgeSplitModifierData *temd = (EdgeSplitModifierData*) target;
134 temd->split_angle = emd->split_angle;
135 temd->flags = emd->flags;
138 static DerivedMesh *edgesplitModifier_do(EdgeSplitModifierData *emd,
139 Object *ob, DerivedMesh *dm)
141 if(!(emd->flags & (MOD_EDGESPLIT_FROMANGLE | MOD_EDGESPLIT_FROMFLAG)))
144 return doEdgeSplit(dm, emd, ob);
147 static DerivedMesh *applyModifier(
148 ModifierData *md, Object *ob, DerivedMesh *derivedData,
149 int UNUSED(useRenderParams), int UNUSED(isFinalCalc))
152 EdgeSplitModifierData *emd = (EdgeSplitModifierData*) md;
154 result = edgesplitModifier_do(emd, ob, derivedData);
156 if(result != derivedData)
157 CDDM_calc_normals(result);
162 static DerivedMesh *applyModifierEM(ModifierData *md, Object *ob,
163 BMEditMesh *UNUSED(editData), DerivedMesh *derivedData)
165 return applyModifier(md, ob, derivedData, 0, 1);
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,
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,
190 /* isDisabled */ NULL,
191 /* updateDepgraph */ NULL,
192 /* dependsOnTime */ NULL,
193 /* dependsOnNormals */ NULL,
194 /* foreachObjectLink */ NULL,
195 /* foreachIDLink */ NULL,
196 /* foreachTexLink */ NULL,