8d93980a9d042e3fc5620dc11b22f97233b3b04e
[blender.git] / source / blender / gpencil_modifiers / intern / MOD_gpencilsmooth.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) 2017, Blender Foundation
19  * This is a new part of Blender
20  *
21  * Contributor(s): Antonio Vazquez
22  *
23  * ***** END GPL LICENSE BLOCK *****
24  *
25  */
26
27 /** \file blender/gpencil_modifiers/intern/MOD_gpencilsmooth.c
28  *  \ingroup modifiers
29  */
30
31 #include <stdio.h>
32
33 #include "DNA_meshdata_types.h"
34 #include "DNA_object_types.h"
35 #include "DNA_gpencil_types.h"
36 #include "DNA_gpencil_modifier_types.h"
37
38 #include "BKE_context.h"
39 #include "BKE_deform.h"
40 #include "BKE_gpencil.h"
41 #include "BKE_gpencil_modifier.h"
42
43 #include "DEG_depsgraph.h"
44
45 #include "MOD_gpencil_util.h"
46 #include "MOD_gpencil_modifiertypes.h"
47
48 static void initData(GpencilModifierData *md)
49 {
50         SmoothGpencilModifierData *gpmd = (SmoothGpencilModifierData *)md;
51         gpmd->pass_index = 0;
52         gpmd->flag |= GP_SMOOTH_MOD_LOCATION;
53         gpmd->factor = 0.5f;
54         gpmd->layername[0] = '\0';
55         gpmd->vgname[0] = '\0';
56         gpmd->step = 1;
57 }
58
59 static void copyData(const GpencilModifierData *md, GpencilModifierData *target)
60 {
61         BKE_gpencil_modifier_copyData_generic(md, target);
62 }
63
64 /* aply smooth effect based on stroke direction */
65 static void deformStroke(
66         GpencilModifierData *md, Depsgraph *UNUSED(depsgraph),
67         Object *ob, bGPDlayer *gpl, bGPDstroke *gps)
68 {
69         SmoothGpencilModifierData *mmd = (SmoothGpencilModifierData *)md;
70         const int def_nr = defgroup_name_index(ob, mmd->vgname);
71
72         if (!is_stroke_affected_by_modifier(
73                     ob,
74                     mmd->layername, mmd->pass_index, mmd->layer_pass, 3, gpl, gps,
75                     mmd->flag & GP_SMOOTH_INVERT_LAYER, mmd->flag & GP_SMOOTH_INVERT_PASS,
76                     mmd->flag & GP_SMOOTH_INVERT_LAYERPASS))
77         {
78                 return;
79         }
80
81         /* smooth stroke */
82         if (mmd->factor > 0.0f) {
83                 for (int r = 0; r < mmd->step; r++) {
84                         for (int i = 0; i < gps->totpoints; i++) {
85                                 MDeformVert *dvert = gps->dvert != NULL ? &gps->dvert[i] : NULL;
86
87                                 /* verify vertex group */
88                                 const float weight = get_modifier_point_weight(
89                                         dvert, (mmd->flag & GP_SMOOTH_INVERT_VGROUP) != 0, def_nr);
90                                 if (weight < 0.0f) {
91                                         continue;
92                                 }
93
94                                 const float val = mmd->factor * weight;
95                                 /* perform smoothing */
96                                 if (mmd->flag & GP_SMOOTH_MOD_LOCATION) {
97                                         BKE_gpencil_smooth_stroke(gps, i, val);
98                                 }
99                                 if (mmd->flag & GP_SMOOTH_MOD_STRENGTH) {
100                                         BKE_gpencil_smooth_stroke_strength(gps, i, val);
101                                 }
102                                 if ((mmd->flag & GP_SMOOTH_MOD_THICKNESS)  && (val > 0.0f)) {
103                                         /* thickness need to repeat process several times */
104                                         for (int r2 = 0; r2 < r * 10; r2++) {
105                                                 BKE_gpencil_smooth_stroke_thickness(gps, i, val);
106                                         }
107                                 }
108                                 if (mmd->flag & GP_SMOOTH_MOD_UV) {
109                                         BKE_gpencil_smooth_stroke_uv(gps, i, val);
110                                 }
111                         }
112                 }
113         }
114 }
115
116 static void bakeModifier(
117         struct Main *UNUSED(bmain), Depsgraph *depsgraph,
118         GpencilModifierData *md, Object *ob)
119 {
120         bGPdata *gpd = ob->data;
121
122         for (bGPDlayer *gpl = gpd->layers.first; gpl; gpl = gpl->next) {
123                 for (bGPDframe *gpf = gpl->frames.first; gpf; gpf = gpf->next) {
124                         for (bGPDstroke *gps = gpf->strokes.first; gps; gps = gps->next) {
125                                 deformStroke(md, depsgraph, ob, gpl, gps);
126                         }
127                 }
128         }
129 }
130
131 GpencilModifierTypeInfo modifierType_Gpencil_Smooth = {
132         /* name */              "Smooth",
133         /* structName */        "SmoothGpencilModifierData",
134         /* structSize */        sizeof(SmoothGpencilModifierData),
135         /* type */              eGpencilModifierTypeType_Gpencil,
136         /* flags */             eGpencilModifierTypeFlag_SupportsEditmode,
137
138         /* copyData */          copyData,
139
140         /* deformStroke */      deformStroke,
141         /* generateStrokes */   NULL,
142         /* bakeModifier */      bakeModifier,
143         /* remapTime */         NULL,
144
145         /* initData */          initData,
146         /* freeData */          NULL,
147         /* isDisabled */        NULL,
148         /* updateDepsgraph */   NULL,
149         /* dependsOnTime */     NULL,
150         /* foreachObjectLink */ NULL,
151         /* foreachIDLink */     NULL,
152         /* foreachTexLink */    NULL,
153 };