Welcome to mirror list, hosted at ThFree Co, Russian Federation.

sculpt_filter_mask.c « sculpt_paint « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: ab862ad150833d2908362b4a00f57a9996600b29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2020 Blender Foundation.
 * All rights reserved.
 */

/** \file
 * \ingroup edsculpt
 */

#include "MEM_guardedalloc.h"

#include "BLI_blenlib.h"
#include "BLI_hash.h"
#include "BLI_math.h"
#include "BLI_task.h"

#include "DNA_mesh_types.h"
#include "DNA_meshdata_types.h"

#include "BKE_brush.h"
#include "BKE_context.h"
#include "BKE_mesh.h"
#include "BKE_mesh_mapping.h"
#include "BKE_object.h"
#include "BKE_paint.h"
#include "BKE_pbvh.h"
#include "BKE_scene.h"

#include "DEG_depsgraph.h"

#include "WM_api.h"
#include "WM_message.h"
#include "WM_toolsystem.h"
#include "WM_types.h"

#include "ED_object.h"
#include "ED_screen.h"
#include "ED_sculpt.h"
#include "paint_intern.h"
#include "sculpt_intern.h"

#include "RNA_access.h"
#include "RNA_define.h"

#include "UI_interface.h"

#include "bmesh.h"

#include <math.h>
#include <stdlib.h>

typedef enum eSculptMaskFilterTypes {
  MASK_FILTER_SMOOTH = 0,
  MASK_FILTER_SHARPEN = 1,
  MASK_FILTER_GROW = 2,
  MASK_FILTER_SHRINK = 3,
  MASK_FILTER_CONTRAST_INCREASE = 5,
  MASK_FILTER_CONTRAST_DECREASE = 6,
} eSculptMaskFilterTypes;

static EnumPropertyItem prop_mask_filter_types[] = {
    {MASK_FILTER_SMOOTH, "SMOOTH", 0, "Smooth Mask", "Smooth mask"},
    {MASK_FILTER_SHARPEN, "SHARPEN", 0, "Sharpen Mask", "Sharpen mask"},
    {MASK_FILTER_GROW, "GROW", 0, "Grow Mask", "Grow mask"},
    {MASK_FILTER_SHRINK, "SHRINK", 0, "Shrink Mask", "Shrink mask"},
    {MASK_FILTER_CONTRAST_INCREASE,
     "CONTRAST_INCREASE",
     0,
     "Increase Contrast",
     "Increase the contrast of the paint mask"},
    {MASK_FILTER_CONTRAST_DECREASE,
     "CONTRAST_DECREASE",
     0,
     "Decrease Contrast",
     "Decrease the contrast of the paint mask"},
    {0, NULL, 0, NULL, NULL},
};

static void mask_filter_task_cb(void *__restrict userdata,
                                const int i,
                                const TaskParallelTLS *__restrict UNUSED(tls))
{
  SculptThreadedTaskData *data = userdata;
  SculptSession *ss = data->ob->sculpt;
  PBVHNode *node = data->nodes[i];
  bool update = false;

  const int mode = data->filter_type;
  float contrast = 0.0f;

  PBVHVertexIter vd;

  if (mode == MASK_FILTER_CONTRAST_INCREASE) {
    contrast = 0.1f;
  }

  if (mode == MASK_FILTER_CONTRAST_DECREASE) {
    contrast = -0.1f;
  }

  BKE_pbvh_vertex_iter_begin (ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
    float delta, gain, offset, max, min;
    float prev_val = *vd.mask;
    SculptVertexNeighborIter ni;
    switch (mode) {
      case MASK_FILTER_SMOOTH:
      case MASK_FILTER_SHARPEN: {
        float val = SCULPT_neighbor_mask_average(ss, vd.vertex);

        val -= *vd.mask;

        if (mode == MASK_FILTER_SMOOTH) {
          *vd.mask += val;
        }
        else if (mode == MASK_FILTER_SHARPEN) {
          if (*vd.mask > 0.5f) {
            *vd.mask += 0.05f;
          }
          else {
            *vd.mask -= 0.05f;
          }
          *vd.mask += val / 2.0f;
        }
        break;
      }
      case MASK_FILTER_GROW:
        max = 0.0f;
        SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vd.vertex, ni) {
          float vmask_f = data->prev_mask[ni.index];
          if (vmask_f > max) {
            max = vmask_f;
          }
        }
        SCULPT_VERTEX_NEIGHBORS_ITER_END(ni);
        *vd.mask = max;
        break;
      case MASK_FILTER_SHRINK:
        min = 1.0f;
        SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vd.vertex, ni) {
          float vmask_f = data->prev_mask[ni.index];
          if (vmask_f < min) {
            min = vmask_f;
          }
        }
        SCULPT_VERTEX_NEIGHBORS_ITER_END(ni);
        *vd.mask = min;
        break;
      case MASK_FILTER_CONTRAST_INCREASE:
      case MASK_FILTER_CONTRAST_DECREASE:
        delta = contrast / 2.0f;
        gain = 1.0f - delta * 2.0f;
        if (contrast > 0) {
          gain = 1.0f / ((gain != 0.0f) ? gain : FLT_EPSILON);
          offset = gain * (-delta);
        }
        else {
          delta *= -1.0f;
          offset = gain * (delta);
        }
        *vd.mask = gain * (*vd.mask) + offset;
        break;
    }
    *vd.mask = clamp_f(*vd.mask, 0.0f, 1.0f);
    if (*vd.mask != prev_val) {
      update = true;
    }
    if (vd.mvert) {
      vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
    }
  }
  BKE_pbvh_vertex_iter_end;

  if (update) {
    BKE_pbvh_node_mark_update_mask(node);
  }
}

static int sculpt_mask_filter_exec(bContext *C, wmOperator *op)
{
  Object *ob = CTX_data_active_object(C);
  SculptSession *ss = ob->sculpt;
  Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
  PBVH *pbvh = ob->sculpt->pbvh;
  PBVHNode **nodes;
  Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
  int totnode;
  int filter_type = RNA_enum_get(op->ptr, "filter_type");

  BKE_sculpt_update_object_for_edit(depsgraph, ob, true, true, false);

  SCULPT_vertex_random_access_ensure(ss);

  if (!ob->sculpt->pmap) {
    return OPERATOR_CANCELLED;
  }

  int num_verts = SCULPT_vertex_count_get(ss);

  BKE_pbvh_search_gather(pbvh, NULL, NULL, &nodes, &totnode);
  SCULPT_undo_push_begin(ob, "Mask filter");

  for (int i = 0; i < totnode; i++) {
    SCULPT_undo_push_node(ob, nodes[i], SCULPT_UNDO_MASK);
  }

  float *prev_mask = NULL;
  int iterations = RNA_int_get(op->ptr, "iterations");

  /* Auto iteration count calculates the number of iteration based on the vertices of the mesh to
   * avoid adding an unnecessary amount of undo steps when using the operator from a shortcut.
   * One iteration per 50000 vertices in the mesh should be fine in most cases.
   * Maybe we want this to be configurable. */
  if (RNA_boolean_get(op->ptr, "auto_iteration_count")) {
    iterations = (int)(num_verts / 50000.0f) + 1;
  }

  for (int i = 0; i < iterations; i++) {
    if (ELEM(filter_type, MASK_FILTER_GROW, MASK_FILTER_SHRINK)) {
      prev_mask = MEM_mallocN(num_verts * sizeof(float), "prevmask");
      for (int j = 0; j < num_verts; j++) {
        SculptVertRef vertex = BKE_pbvh_table_index_to_vertex(ss->pbvh, j);

        prev_mask[j] = SCULPT_vertex_mask_get(ss, vertex);
      }
    }

    SculptThreadedTaskData data = {
        .sd = sd,
        .ob = ob,
        .nodes = nodes,
        .filter_type = filter_type,
        .prev_mask = prev_mask,
    };

    TaskParallelSettings settings;
    BKE_pbvh_parallel_range_settings(&settings, true, totnode);
    BLI_task_parallel_range(0, totnode, &data, mask_filter_task_cb, &settings);

    if (ELEM(filter_type, MASK_FILTER_GROW, MASK_FILTER_SHRINK)) {
      MEM_freeN(prev_mask);
    }
  }

  MEM_SAFE_FREE(nodes);

  SCULPT_undo_push_end();

  SCULPT_tag_update_overlays(C);

  return OPERATOR_FINISHED;
}

void SCULPT_mask_filter_smooth_apply(
    Sculpt *sd, Object *ob, PBVHNode **nodes, const int totnode, const int smooth_iterations)
{
  SculptThreadedTaskData data = {
      .sd = sd,
      .ob = ob,
      .nodes = nodes,
      .filter_type = MASK_FILTER_SMOOTH,
  };

  for (int i = 0; i < smooth_iterations; i++) {
    TaskParallelSettings settings;
    BKE_pbvh_parallel_range_settings(&settings, true, totnode);
    BLI_task_parallel_range(0, totnode, &data, mask_filter_task_cb, &settings);
  }
}

void SCULPT_OT_mask_filter(struct wmOperatorType *ot)
{
  /* Identifiers. */
  ot->name = "Mask Filter";
  ot->idname = "SCULPT_OT_mask_filter";
  ot->description = "Applies a filter to modify the current mask";

  /* API callbacks. */
  ot->exec = sculpt_mask_filter_exec;
  ot->poll = SCULPT_mode_poll;

  ot->flag = OPTYPE_REGISTER;

  /* RNA. */
  RNA_def_enum(ot->srna,
               "filter_type",
               prop_mask_filter_types,
               MASK_FILTER_SMOOTH,
               "Type",
               "Filter that is going to be applied to the mask");
  RNA_def_int(ot->srna,
              "iterations",
              1,
              1,
              100,
              "Iterations",
              "Number of times that the filter is going to be applied",
              1,
              100);
  RNA_def_boolean(
      ot->srna,
      "auto_iteration_count",
      false,
      "Auto Iteration Count",
      "Use a automatic number of iterations based on the number of vertices of the sculpt");
}

/******************************************************************************************/
/* Interactive Preview Mask Filter */

#define SCULPT_IPMASK_FILTER_MIN_MULTITHREAD 1000
#define SCULPT_IPMASK_FILTER_GRANULARITY 100

#define SCULPT_IPMASK_FILTER_QUANTIZE_STEP 0.1

typedef enum eSculptIPMaskFilterType {
  IPMASK_FILTER_SMOOTH_SHARPEN,
  IPMASK_FILTER_GROW_SHRINK,
  IPMASK_FILTER_HARDER_SOFTER,
  IPMASK_FILTER_CONTRAST,
  IPMASK_FILTER_ADD_SUBSTRACT,
  IPMASK_FILTER_INVERT,
  IPMASK_FILTER_QUANTIZE,
} eSculptIPMaskFilterType;

typedef enum MaskFilterStepDirectionType {
  MASK_FILTER_STEP_DIRECTION_FORWARD,
  MASK_FILTER_STEP_DIRECTION_BACKWARD,
} MaskFilterStepDirectionType;

/* Grown/Shrink vertex callbacks. */
static float sculpt_ipmask_vertex_grow_cb(SculptSession *ss,
                                          const SculptVertRef vertex,
                                          float *current_mask)
{
  int vertex_i = BKE_pbvh_vertex_index_to_table(ss->pbvh, vertex);

  float max = 0.0f;
  SculptVertexNeighborIter ni;
  SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vertex, ni) {
    float vmask_f = current_mask[ni.index];
    if (vmask_f > max) {
      max = vmask_f;
    }
  }
  SCULPT_VERTEX_NEIGHBORS_ITER_END(ni);
  return max;
}

static float sculpt_ipmask_vertex_shrink_cb(SculptSession *ss,
                                            const SculptVertRef vertex,
                                            float *current_mask)
{
  int vertex_i = BKE_pbvh_vertex_index_to_table(ss->pbvh, vertex);

  float min = 1.0f;
  SculptVertexNeighborIter ni;
  SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vertex, ni) {
    float vmask_f = current_mask[ni.index];
    if (vmask_f < min) {
      min = vmask_f;
    }
  }
  SCULPT_VERTEX_NEIGHBORS_ITER_END(ni);
  return min;
}

/* Smooth/Sharpen vertex callbacks. */
static float sculpt_ipmask_vertex_smooth_cb(SculptSession *ss,
                                            const SculptVertRef vertex,
                                            float *current_mask)
{
  int vertex_i = BKE_pbvh_vertex_index_to_table(ss->pbvh, vertex);

  float accum = current_mask[vertex_i];
  int total = 1;
  SculptVertexNeighborIter ni;
  SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vertex, ni) {
    accum += current_mask[ni.index];
    total++;
  }
  SCULPT_VERTEX_NEIGHBORS_ITER_END(ni);
  return total > 0 ? accum / total : current_mask[vertex_i];
}

static float sculpt_ipmask_vertex_sharpen_cb(SculptSession *ss,
                                             const SculptVertRef vertex,
                                             float *current_mask)
{
  int vertex_i = BKE_pbvh_vertex_index_to_table(ss->pbvh, vertex);

  float accum = 0.0f;
  int total = 0;
  float vmask = current_mask[vertex_i];
  SculptVertexNeighborIter ni;
  SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vertex, ni) {
    accum += current_mask[ni.index];
    total++;
  }
  SCULPT_VERTEX_NEIGHBORS_ITER_END(ni);
  const float avg = total > 0 ? accum / total : current_mask[vertex_i];
  const float val = avg - vmask;

  float new_mask;
  if (vmask > 0.5f) {
    new_mask = vmask + 0.03f;
  }
  else {
    new_mask = vmask - 0.03f;
  }
  new_mask += val / 2.0f;
  return clamp_f(new_mask, 0.0f, 1.0f);

#ifdef SHARP_KERNEL
  float accum = 0.0f;
  float weight_accum = 0.0f;
  const float neighbor_weight = -1.0f;
  int neighbor_count = 0;

  SculptVertexNeighborIter ni;
  SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vertex, ni) {
    accum += neighbor_weight * current_mask[ni.index];
    weight_accum += neighbor_weight;
    neighbor_count++;
  }
  SCULPT_VERTEX_NEIGHBORS_ITER_END(ni);

  const float main_weight = (neighbor_count + 1) * 2.0f;
  accum += main_weight * current_mask[vertex];
  weight_accum += main_weight;

  return clamp_f(accum / weight_accum, 0.0f, 1.0f);
#endif
}

/* Harder/Softer callbacks. */
#define SCULPT_IPMASK_FILTER_HARDER_SOFTER_STEP 0.01f
static float sculpt_ipmask_vertex_harder_cb(SculptSession *ss,
                                            const SculptVertRef vertex,
                                            float *current_mask)
{
  int vertex_i = BKE_pbvh_vertex_index_to_table(ss->pbvh, vertex);

  return clamp_f(current_mask[vertex_i] += current_mask[vertex_i] *
                                           SCULPT_IPMASK_FILTER_HARDER_SOFTER_STEP,
                 0.0f,
                 1.0f);
}

static float sculpt_ipmask_vertex_softer_cb(SculptSession *ss,
                                            const SculptVertRef vertex,
                                            float *current_mask)
{
  int vertex_i = BKE_pbvh_vertex_index_to_table(ss->pbvh, vertex);

  return clamp_f(current_mask[vertex_i] -= current_mask[vertex_i] *
                                           SCULPT_IPMASK_FILTER_HARDER_SOFTER_STEP,
                 0.0f,
                 1.0f);
}

/* Contrast Increase/Decrease callbacks. */

#define SCULPT_IPMASK_FILTER_CONTRAST_STEP 0.05f
static float sculpt_ipmask_filter_contrast(const float mask, const float contrast)
{
  float offset;
  float delta = contrast / 2.0f;
  float gain = 1.0f - delta * 2.0f;
  if (contrast > 0.0f) {
    gain = 1.0f / ((gain != 0.0f) ? gain : FLT_EPSILON);
    offset = gain * (-delta);
  }
  else {
    delta *= -1.0f;
    offset = gain * (delta);
  }
  return clamp_f(gain * mask + offset, 0.0f, 1.0f);
}

static float sculpt_ipmask_vertex_contrast_increase_cb(SculptSession *ss,
                                                       const SculptVertRef vertex,
                                                       float *current_mask)
{
  int vertex_i = BKE_pbvh_vertex_index_to_table(ss->pbvh, vertex);

  return sculpt_ipmask_filter_contrast(current_mask[vertex_i], SCULPT_IPMASK_FILTER_CONTRAST_STEP);
}

static float sculpt_ipmask_vertex_contrast_decrease_cb(SculptSession *ss,
                                                       const SculptVertRef vertex,
                                                       float *current_mask)
{
  int vertex_i = BKE_pbvh_vertex_index_to_table(ss->pbvh, vertex);

  return sculpt_ipmask_filter_contrast(current_mask[vertex_i],
                                       -1.0f * SCULPT_IPMASK_FILTER_CONTRAST_STEP);
}

static MaskFilterDeltaStep *sculpt_ipmask_filter_delta_create(const float *current_mask,
                                                              const float *next_mask,
                                                              const int totvert)
{
  int tot_modified_values = 0;
  for (int i = 0; i < totvert; i++) {
    if (current_mask[i] == next_mask[i]) {
      continue;
    }
    tot_modified_values++;
  }

  MaskFilterDeltaStep *delta_step = MEM_callocN(sizeof(MaskFilterDeltaStep),
                                                "mask filter delta step");
  delta_step->totelem = tot_modified_values;
  delta_step->index = MEM_malloc_arrayN(sizeof(int), tot_modified_values, "delta indices");
  delta_step->delta = MEM_malloc_arrayN(sizeof(float), tot_modified_values, "delta values");

  int delta_step_index = 0;
  for (int i = 0; i < totvert; i++) {
    if (current_mask[i] == next_mask[i]) {
      continue;
    }
    delta_step->index[delta_step_index] = i;
    delta_step->delta[delta_step_index] = next_mask[i] - current_mask[i];
    delta_step_index++;
  }
  return delta_step;
}

typedef struct SculptIPMaskFilterTaskData {
  SculptSession *ss;
  float *next_mask;
  float *current_mask;
  MaskFilterStepDirectionType direction;
} SculptIPMaskFilterTaskData;

static void ipmask_filter_compute_step_task_cb(void *__restrict userdata,
                                               const int i,
                                               const TaskParallelTLS *__restrict UNUSED(tls))
{
  SculptIPMaskFilterTaskData *data = userdata;
  SculptVertRef vertex = BKE_pbvh_table_index_to_vertex(data->ss->pbvh, i);

  if (data->direction == MASK_FILTER_STEP_DIRECTION_FORWARD) {
    data->next_mask[i] = data->ss->filter_cache->mask_filter_step_forward(
        data->ss, vertex, data->current_mask);
  }
  else {
    data->next_mask[i] = data->ss->filter_cache->mask_filter_step_backward(
        data->ss, vertex, data->current_mask);
  }
}

static float *sculpt_ipmask_step_compute(SculptSession *ss,
                                         float *current_mask,
                                         MaskFilterStepDirectionType direction)
{
  const int totvert = SCULPT_vertex_count_get(ss);
  float *next_mask = MEM_malloc_arrayN(sizeof(float), totvert, "delta values");

  SculptIPMaskFilterTaskData data = {
      .ss = ss,
      .next_mask = next_mask,
      .current_mask = current_mask,
      .direction = direction,
  };
  TaskParallelSettings settings;
  memset(&settings, 0, sizeof(TaskParallelSettings));
  settings.use_threading = totvert > SCULPT_IPMASK_FILTER_MIN_MULTITHREAD;
  settings.min_iter_per_thread = SCULPT_IPMASK_FILTER_GRANULARITY;
  BLI_task_parallel_range(0, totvert, &data, ipmask_filter_compute_step_task_cb, &settings);

  return next_mask;
}

static float *sculpt_ipmask_current_state_get(SculptSession *ss)
{
  return MEM_dupallocN(ss->filter_cache->mask_filter_ref);
}

static void sculpt_ipmask_reference_set(SculptSession *ss, float *new_mask)
{
  const int totvert = SCULPT_vertex_count_get(ss);
  for (int i = 0; i < totvert; i++) {
    ss->filter_cache->mask_filter_ref[i] = new_mask[i];
  }
}

static void sculpt_ipmask_store_reference_step(SculptSession *ss)
{
  const int totvert = SCULPT_vertex_count_get(ss);
  if (!ss->filter_cache->mask_filter_ref) {
    ss->filter_cache->mask_filter_ref = MEM_malloc_arrayN(sizeof(float), totvert, "delta values");
  }

  for (int i = 0; i < totvert; i++) {
    SculptVertRef vertex = BKE_pbvh_table_index_to_vertex(ss->pbvh, i);

    ss->filter_cache->mask_filter_ref[i] = SCULPT_vertex_mask_get(ss, vertex);
  }
}

static void ipmask_filter_apply_task_cb(void *__restrict userdata,
                                        const int i,
                                        const TaskParallelTLS *__restrict UNUSED(tls))
{
  SculptThreadedTaskData *data = userdata;
  SculptSession *ss = data->ss;
  FilterCache *filter_cache = ss->filter_cache;
  PBVHNode *node = filter_cache->nodes[i];
  PBVHVertexIter vd;
  bool update = false;
  BKE_pbvh_vertex_iter_begin (ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
    if (SCULPT_automasking_factor_get(filter_cache->automasking, ss, vd.vertex) < 0.5f) {
      continue;
    }

    float new_mask;
    if (data->next_mask) {
      new_mask = interpf(
          data->next_mask[vd.index], data->new_mask[vd.index], data->mask_interpolation);
    }
    else {
      new_mask = data->new_mask[vd.index];
    }

    if (*vd.mask == new_mask) {
      continue;
    }

    *vd.mask = new_mask;
    update = true;
    if (vd.mvert) {
      vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
    }
  }
  BKE_pbvh_vertex_iter_end;

  if (update) {
    BKE_pbvh_node_mark_redraw(node);
  }
}

static void sculpt_ipmask_apply_mask_data(SculptSession *ss,
                                          float *new_mask,
                                          float *next_mask,
                                          const float interpolation)
{
  FilterCache *filter_cache = ss->filter_cache;
  SculptThreadedTaskData data = {
      .ss = ss,
      .nodes = filter_cache->nodes,
      .new_mask = new_mask,
      .next_mask = next_mask,
      .mask_interpolation = interpolation,
  };

  TaskParallelSettings settings;
  BKE_pbvh_parallel_range_settings(&settings, true, filter_cache->totnode);
  BLI_task_parallel_range(0, filter_cache->totnode, &data, ipmask_filter_apply_task_cb, &settings);
}

static float *sculpt_ipmask_apply_delta_step(MaskFilterDeltaStep *delta_step,
                                             const float *current_mask,
                                             const MaskFilterStepDirectionType direction)
{
  float *next_mask = MEM_dupallocN(current_mask);
  for (int i = 0; i < delta_step->totelem; i++) {
    if (direction == MASK_FILTER_STEP_DIRECTION_FORWARD) {
      next_mask[delta_step->index[i]] = current_mask[delta_step->index[i]] + delta_step->delta[i];
    }
    else {
      next_mask[delta_step->index[i]] = current_mask[delta_step->index[i]] - delta_step->delta[i];
    }
  }
  return next_mask;
}

static float *sculpt_ipmask_restore_state_from_delta(SculptSession *ss,
                                                     MaskFilterDeltaStep *delta_step,
                                                     MaskFilterStepDirectionType direction)
{
  float *current_mask = sculpt_ipmask_current_state_get(ss);
  float *next_mask = sculpt_ipmask_apply_delta_step(delta_step, current_mask, direction);
  MEM_freeN(current_mask);
  return next_mask;
}

static float *sculpt_ipmask_compute_and_store_step(SculptSession *ss,
                                                   const int iterations,
                                                   const int delta_index,
                                                   MaskFilterStepDirectionType direction)
{
  BLI_assert(iterations > 0);
  const int totvert = SCULPT_vertex_count_get(ss);
  float *current_mask = sculpt_ipmask_current_state_get(ss);
  float *original_mask = MEM_dupallocN(current_mask);
  float *next_mask = NULL;

  /* Compute the filter. */
  for (int i = 0; i < iterations; i++) {
    MEM_SAFE_FREE(next_mask);
    next_mask = sculpt_ipmask_step_compute(ss, current_mask, direction);
    MEM_freeN(current_mask);
    current_mask = MEM_dupallocN(next_mask);
  }
  MEM_freeN(current_mask);

  /* Pack and store the delta step. */
  MaskFilterDeltaStep *delta_step;
  if (direction == MASK_FILTER_STEP_DIRECTION_FORWARD) {
    delta_step = sculpt_ipmask_filter_delta_create(original_mask, next_mask, totvert);
  }
  else {
    delta_step = sculpt_ipmask_filter_delta_create(next_mask, original_mask, totvert);
  }
  BLI_ghash_insert(ss->filter_cache->mask_delta_step, POINTER_FROM_INT(delta_index), delta_step);
  MEM_freeN(original_mask);

  return next_mask;
}

static float *sculpt_ipmask_filter_mask_for_step_get(SculptSession *ss,
                                                     MaskFilterStepDirectionType direction,
                                                     const int iteration_count)
{
  FilterCache *filter_cache = ss->filter_cache;
  int next_step = filter_cache->mask_filter_current_step;
  int delta_index = next_step;
  /* Get the next step and the delta step index associated with it. */
  if (direction == MASK_FILTER_STEP_DIRECTION_FORWARD) {
    next_step = filter_cache->mask_filter_current_step + 1;
    delta_index = filter_cache->mask_filter_current_step;
  }
  else {
    next_step = filter_cache->mask_filter_current_step - 1;
    delta_index = filter_cache->mask_filter_current_step - 1;
  }

  /* Update the data one step forward/backward. */
  if (BLI_ghash_haskey(filter_cache->mask_delta_step, POINTER_FROM_INT(delta_index))) {
    /* This step was already computed, restore it from the current step and a delta. */
    MaskFilterDeltaStep *delta_step = BLI_ghash_lookup(filter_cache->mask_delta_step,
                                                       POINTER_FROM_INT(delta_index));
    return sculpt_ipmask_restore_state_from_delta(ss, delta_step, direction);
  }

  /* New step that was not yet computed. Compute and store the delta. */
  return sculpt_ipmask_compute_and_store_step(ss, iteration_count, delta_index, direction);
}

static void sculpt_ipmask_filter_update_to_target_step(SculptSession *ss,
                                                       const int target_step,
                                                       const int iteration_count,
                                                       const float step_interpolation)
{
  FilterCache *filter_cache = ss->filter_cache;

  MaskFilterStepDirectionType direction;
  /* Get the next step and the delta step index associated with it. */
  if (target_step > filter_cache->mask_filter_current_step) {
    direction = MASK_FILTER_STEP_DIRECTION_FORWARD;
  }
  else {
    direction = MASK_FILTER_STEP_DIRECTION_BACKWARD;
  }

  while (filter_cache->mask_filter_current_step != target_step) {
    /* Restore or compute a mask in the given direction. */
    float *new_mask = sculpt_ipmask_filter_mask_for_step_get(ss, direction, iteration_count);

    /* Store the full step. */
    sculpt_ipmask_reference_set(ss, new_mask);
    MEM_freeN(new_mask);

    /* Update the current step count. */
    if (direction == MASK_FILTER_STEP_DIRECTION_FORWARD) {
      filter_cache->mask_filter_current_step += 1;
    }
    else {
      filter_cache->mask_filter_current_step -= 1;
    }
  }

  if (step_interpolation != 0.0f) {
    float *next_mask = sculpt_ipmask_filter_mask_for_step_get(
        ss, MASK_FILTER_STEP_DIRECTION_FORWARD, iteration_count);
    sculpt_ipmask_apply_mask_data(
        ss, filter_cache->mask_filter_ref, next_mask, step_interpolation);
    MEM_freeN(next_mask);
  }
  else {
    sculpt_ipmask_apply_mask_data(ss, filter_cache->mask_filter_ref, NULL, 0.0f);
  }
}

static void ipmask_filter_apply_from_original_task_cb(
    void *__restrict userdata, const int i, const TaskParallelTLS *__restrict UNUSED(tls))
{
  SculptThreadedTaskData *data = userdata;
  SculptSession *ss = data->ss;
  FilterCache *filter_cache = ss->filter_cache;
  PBVHNode *node = filter_cache->nodes[i];
  PBVHVertexIter vd;
  SculptOrigVertData orig_data;
  const eSculptIPMaskFilterType filter_type = data->filter_type;
  bool update = false;

  /* Used for quantize filter. */
  const int steps = data->filter_strength / SCULPT_IPMASK_FILTER_QUANTIZE_STEP;
  if (steps == 0) {
    return;
  }
  const float step_size = 1.0f / steps;

  SCULPT_orig_vert_data_init(&orig_data, data->ob, node, SCULPT_UNDO_COORDS);
  BKE_pbvh_vertex_iter_begin (ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
    if (SCULPT_automasking_factor_get(filter_cache->automasking, ss, vd.vertex) < 0.5f) {
      continue;
    }
    SCULPT_orig_vert_data_update(&orig_data, vd.vertex);
    float new_mask = orig_data.mask;
    switch (filter_type) {
      case IPMASK_FILTER_ADD_SUBSTRACT:
        new_mask = orig_data.mask + data->filter_strength;
        break;
      case IPMASK_FILTER_INVERT: {
        const float strength = clamp_f(data->filter_strength, 0.0f, 1.0f);
        const float mask_invert = 1.0f - orig_data.mask;
        new_mask = interpf(mask_invert, orig_data.mask, strength);
        break;
      }
      case IPMASK_FILTER_QUANTIZE: {
        const float remainder = fmod(orig_data.mask, step_size);
        const float total_steps = (orig_data.mask - remainder) / step_size;
        new_mask = total_steps * step_size;
        break;
      }
      default:
        BLI_assert(false);
        break;
    }
    new_mask = clamp_f(new_mask, 0.0f, 1.0f);
    if (*vd.mask == new_mask) {
      continue;
    }

    *vd.mask = new_mask;
    update = true;
    if (vd.mvert) {
      vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
    }
  }
  BKE_pbvh_vertex_iter_end;

  if (update) {
    BKE_pbvh_node_mark_redraw(node);
  }
}

static void sculpt_ipmask_apply_from_original_mask_data(Object *ob,
                                                        eSculptIPMaskFilterType filter_type,
                                                        const float strength)
{
  SculptSession *ss = ob->sculpt;
  FilterCache *filter_cache = ss->filter_cache;
  SculptThreadedTaskData data = {
      .ob = ob,
      .ss = ss,
      .nodes = filter_cache->nodes,
      .filter_strength = strength,
      .filter_type = filter_type,
  };

  TaskParallelSettings settings;
  BKE_pbvh_parallel_range_settings(&settings, true, filter_cache->totnode);
  BLI_task_parallel_range(
      0, filter_cache->totnode, &data, ipmask_filter_apply_from_original_task_cb, &settings);
}

static bool sculpt_ipmask_filter_uses_apply_from_original(
    const eSculptIPMaskFilterType filter_type)
{
  return ELEM(
      filter_type, IPMASK_FILTER_INVERT, IPMASK_FILTER_ADD_SUBSTRACT, IPMASK_FILTER_QUANTIZE);
}

static void ipmask_filter_restore_original_mask_task_cb(
    void *__restrict userdata, const int i, const TaskParallelTLS *__restrict UNUSED(tls))
{
  SculptThreadedTaskData *data = userdata;
  SculptSession *ss = data->ss;
  PBVHNode *node = data->nodes[i];
  SculptOrigVertData orig_data;
  bool update = false;
  SCULPT_orig_vert_data_init(&orig_data, data->ob, node, SCULPT_UNDO_COORDS);
  PBVHVertexIter vd;
  BKE_pbvh_vertex_iter_begin (ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
    SCULPT_orig_vert_data_update(&orig_data, vd.vertex);
    *vd.mask = orig_data.mask;
    update = true;
    if (vd.mvert) {
      vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
    }
  }
  BKE_pbvh_vertex_iter_end;

  if (update) {
    BKE_pbvh_node_mark_redraw(node);
  }
}

static void sculpt_ipmask_restore_original_mask(Object *ob)
{
  SculptSession *ss = ob->sculpt;
  FilterCache *filter_cache = ss->filter_cache;
  SculptThreadedTaskData data = {
      .ob = ob,
      .ss = ss,
      .nodes = filter_cache->nodes,
  };

  TaskParallelSettings settings;
  BKE_pbvh_parallel_range_settings(&settings, true, filter_cache->totnode);
  BLI_task_parallel_range(
      0, filter_cache->totnode, &data, ipmask_filter_restore_original_mask_task_cb, &settings);
}

static void sculpt_ipmask_filter_cancel(bContext *C, wmOperator *UNUSED(op))
{
  Object *ob = CTX_data_active_object(C);
  SculptSession *ss = ob->sculpt;

  sculpt_ipmask_restore_original_mask(ob);
  SCULPT_undo_push_end();
  SCULPT_filter_cache_free(ss);
  SCULPT_flush_update_done(C, ob, SCULPT_UPDATE_MASK);
}

#define IPMASK_FILTER_STEP_SENSITIVITY 0.05f
#define IPMASK_FILTER_STEPS_PER_FULL_STRENGTH 20
static int sculpt_ipmask_filter_modal(bContext *C, wmOperator *op, const wmEvent *event)
{
  Object *ob = CTX_data_active_object(C);
  Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
  SculptSession *ss = ob->sculpt;
  FilterCache *filter_cache = ss->filter_cache;
  const int filter_type = RNA_enum_get(op->ptr, "filter_type");
  const bool use_step_interpolation = RNA_boolean_get(op->ptr, "use_step_interpolation");
  const int iteration_count = RNA_int_get(op->ptr, "iterations");

  if ((event->type == EVT_ESCKEY && event->val == KM_PRESS) ||
      (event->type == RIGHTMOUSE && event->val == KM_PRESS)) {
    sculpt_ipmask_filter_cancel(C, op);
    return OPERATOR_FINISHED;
  }

  if (ELEM(event->type, LEFTMOUSE, EVT_RETKEY, EVT_PADENTER)) {
    for (int i = 0; i < filter_cache->totnode; i++) {
      BKE_pbvh_node_mark_update_mask(filter_cache->nodes[i]);
    }
    SCULPT_filter_cache_free(ss);
    SCULPT_undo_push_end();
    SCULPT_flush_update_done(C, ob, SCULPT_UPDATE_MASK);
    return OPERATOR_FINISHED;
  }

  if (event->type != MOUSEMOVE) {
    return OPERATOR_RUNNING_MODAL;
  }

  const float len = event->x - event->prevclickx;
  const float target_step_fl = len * IPMASK_FILTER_STEP_SENSITIVITY * UI_DPI_FAC;
  const int target_step = floorf(target_step_fl);
  const float step_interpolation = use_step_interpolation ? target_step_fl - target_step : 0.0f;
  const float full_step_strength = target_step_fl / IPMASK_FILTER_STEPS_PER_FULL_STRENGTH;

  BKE_sculpt_update_object_for_edit(depsgraph, ob, true, true, false);

  if (sculpt_ipmask_filter_uses_apply_from_original(filter_type)) {
    sculpt_ipmask_apply_from_original_mask_data(ob, filter_type, full_step_strength);
  }
  else {
    sculpt_ipmask_filter_update_to_target_step(
        ss, target_step, iteration_count, step_interpolation);
  }

  SCULPT_tag_update_overlays(C);

  return OPERATOR_RUNNING_MODAL;
}

static void sculpt_ipmask_store_initial_undo_step(Object *ob)
{
  SculptSession *ss = ob->sculpt;
  for (int i = 0; i < ss->filter_cache->totnode; i++) {
    SCULPT_undo_push_node(ob, ss->filter_cache->nodes[i], SCULPT_UNDO_MASK);
  }
}

static FilterCache *sculpt_ipmask_filter_cache_init(Object *ob,
                                                    Sculpt *sd,
                                                    const eSculptIPMaskFilterType filter_type,
                                                    const bool init_automasking)
{
  SculptSession *ss = ob->sculpt;
  FilterCache *filter_cache = MEM_callocN(sizeof(FilterCache), "filter cache");

  filter_cache->active_face_set = SCULPT_FACE_SET_NONE;
  if (init_automasking) {
    filter_cache->automasking = SCULPT_automasking_cache_init(sd, NULL, ob);
  }
  filter_cache->mask_filter_current_step = 0;

  BKE_pbvh_search_gather(ss->pbvh, NULL, NULL, &filter_cache->nodes, &filter_cache->totnode);

  filter_cache->mask_delta_step = BLI_ghash_int_new("mask filter delta steps");
  switch (filter_type) {
    case IPMASK_FILTER_SMOOTH_SHARPEN:
      filter_cache->mask_filter_step_forward = sculpt_ipmask_vertex_smooth_cb;
      filter_cache->mask_filter_step_backward = sculpt_ipmask_vertex_sharpen_cb;
      break;
    case IPMASK_FILTER_GROW_SHRINK:
      filter_cache->mask_filter_step_forward = sculpt_ipmask_vertex_grow_cb;
      filter_cache->mask_filter_step_backward = sculpt_ipmask_vertex_shrink_cb;
      break;
    case IPMASK_FILTER_HARDER_SOFTER:
      filter_cache->mask_filter_step_forward = sculpt_ipmask_vertex_harder_cb;
      filter_cache->mask_filter_step_backward = sculpt_ipmask_vertex_softer_cb;
      break;
    case IPMASK_FILTER_CONTRAST:
      filter_cache->mask_filter_step_forward = sculpt_ipmask_vertex_contrast_increase_cb;
      filter_cache->mask_filter_step_backward = sculpt_ipmask_vertex_contrast_decrease_cb;
      break;
  }

  return filter_cache;
}

static int sculpt_ipmask_filter_invoke(bContext *C, wmOperator *op, const wmEvent *event)
{
  Object *ob = CTX_data_active_object(C);
  Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
  SculptSession *ss = ob->sculpt;
  Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);

  SCULPT_undo_push_begin(ob, "mask filter");

  BKE_sculpt_update_object_for_edit(depsgraph, ob, true, true, false);

  const int filter_type = RNA_enum_get(op->ptr, "filter_type");
  ss->filter_cache = sculpt_ipmask_filter_cache_init(ob, sd, filter_type, true);
  sculpt_ipmask_store_initial_undo_step(ob);
  sculpt_ipmask_store_reference_step(ss);

  WM_event_add_modal_handler(C, op);
  return OPERATOR_RUNNING_MODAL;
}
static int sculpt_ipmask_filter_exec(bContext *C, wmOperator *op)
{
  Object *ob = CTX_data_active_object(C);
  Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
  SculptSession *ss = ob->sculpt;
  Depsgraph *depsgraph = CTX_data_ensure_evaluated_depsgraph(C);

  const int iteration_count = RNA_int_get(op->ptr, "iterations");
  const float strength = RNA_float_get(op->ptr, "strength");
  const int filter_type = RNA_enum_get(op->ptr, "filter_type");
  const int direction = RNA_enum_get(op->ptr, "direction");

  SCULPT_undo_push_begin(ob, "mask filter");
  BKE_sculpt_update_object_for_edit(depsgraph, ob, true, true, false);
  ss->filter_cache = sculpt_ipmask_filter_cache_init(ob, sd, filter_type, false);
  sculpt_ipmask_store_initial_undo_step(ob);
  sculpt_ipmask_store_reference_step(ss);

  const float target_step = direction == MASK_FILTER_STEP_DIRECTION_FORWARD ? 1 : -1;
  if (sculpt_ipmask_filter_uses_apply_from_original(filter_type)) {
    sculpt_ipmask_apply_from_original_mask_data(ob, filter_type, strength * target_step);
  }
  else {
    sculpt_ipmask_filter_update_to_target_step(ss, target_step, iteration_count, 0.0f);
  }

  SCULPT_tag_update_overlays(C);
  SCULPT_filter_cache_free(ss);
  SCULPT_undo_push_end();
  SCULPT_flush_update_done(C, ob, SCULPT_UPDATE_MASK);
  return OPERATOR_FINISHED;
}

void SCULPT_OT_ipmask_filter(struct wmOperatorType *ot)
{
  /* Identifiers. */
  ot->name = "Interactive Preview Mask Filter";
  ot->idname = "SCULPT_OT_ipmask_filter";
  ot->description = "Applies a filter to modify the current mask";

  /* API callbacks. */
  ot->exec = sculpt_ipmask_filter_exec;
  ot->invoke = sculpt_ipmask_filter_invoke;
  ot->modal = sculpt_ipmask_filter_modal;
  ot->cancel = sculpt_ipmask_filter_cancel;
  ot->poll = SCULPT_mode_poll;

  ot->flag = OPTYPE_REGISTER;

  static EnumPropertyItem prop_ipmask_filter_types[] = {
      {IPMASK_FILTER_SMOOTH_SHARPEN,
       "SMOOTH_SHARPEN",
       0,
       "Smooth/Sharpen",
       "Smooth and sharpen the mask"},
      {IPMASK_FILTER_GROW_SHRINK, "GROW_SHRINK", 0, "Grow/Shrink", "Grow and shirnk the mask"},
      {IPMASK_FILTER_HARDER_SOFTER,
       "HARDER_SOFTER",
       0,
       "Harder/Softer",
       "Makes the entire mask harder or softer"},
      {IPMASK_FILTER_ADD_SUBSTRACT,
       "ADD_SUBSTRACT",
       0,
       "Add/Substract",
       "Adds or substract a value to the mask"},
      {IPMASK_FILTER_CONTRAST,
       "CONTRAST",
       0,
       "Contrast",
       "Increases or decreases the contrast of the mask"},
      {IPMASK_FILTER_INVERT, "INVERT", 0, "Invert", "Inverts the mask"},
      {IPMASK_FILTER_QUANTIZE, "QUANTIZE", 0, "Quantize", "Quantizes the mask to intervals"},
      {0, NULL, 0, NULL, NULL},
  };

  static EnumPropertyItem prop_ipmask_filter_direction_types[] = {
      {MASK_FILTER_STEP_DIRECTION_FORWARD,
       "FORWARD",
       0,
       "Forward",
       "Apply the filter in the forward direction"},
      {MASK_FILTER_STEP_DIRECTION_BACKWARD,
       "BACKWARD",
       0,
       "Backward",
       "Apply the filter in the backward direction"},
      {0, NULL, 0, NULL, NULL},
  };

  /* RNA. */
  RNA_def_enum(ot->srna,
               "filter_type",
               prop_ipmask_filter_types,
               IPMASK_FILTER_GROW_SHRINK,
               "Type",
               "Filter that is going to be applied to the mask");
  RNA_def_enum(ot->srna,
               "direction",
               prop_ipmask_filter_direction_types,
               MASK_FILTER_STEP_DIRECTION_FORWARD,
               "Direction",
               "Direction to apply the filter step");
  RNA_def_int(ot->srna,
              "iterations",
              1,
              1,
              100,
              "Iterations per Step",
              "Number of times that the filter is going to be applied per step",
              1,
              100);
  RNA_def_boolean(
      ot->srna,
      "use_step_interpolation",
      true,
      "Step Interpolation",
      "Calculate and render intermediate values between multiple full steps of the filter");
  RNA_def_float(
      ot->srna, "strength", 1.0f, -10.0f, 10.0f, "Strength", "Filter strength", -10.0f, 10.0f);
}

/******************************************************************************************/

static float neighbor_dirty_mask(SculptSession *ss, PBVHVertexIter *vd)
{
  int total = 0;
  float avg[3];
  zero_v3(avg);

  SculptVertexNeighborIter ni;
  SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN (ss, vd->vertex, ni) {
    float normalized[3];
    sub_v3_v3v3(normalized, SCULPT_vertex_co_get(ss, ni.vertex), vd->co);
    normalize_v3(normalized);
    add_v3_v3(avg, normalized);
    total++;
  }
  SCULPT_VERTEX_NEIGHBORS_ITER_END(ni);

  if (total > 0) {
    mul_v3_fl(avg, 1.0f / total);
    float normal[3];
    if (vd->no) {
      normal_short_to_float_v3(normal, vd->no);
    }
    else {
      copy_v3_v3(normal, vd->fno);
    }
    float dot = dot_v3v3(avg, normal);
    float angle = max_ff(saacosf(dot), 0.0f);
    return angle;
  }
  return 0.0f;
}

typedef struct DirtyMaskRangeData {
  float min, max;
} DirtyMaskRangeData;

static void dirty_mask_compute_range_task_cb(void *__restrict userdata,
                                             const int i,
                                             const TaskParallelTLS *__restrict tls)
{
  SculptThreadedTaskData *data = userdata;
  SculptSession *ss = data->ob->sculpt;
  PBVHNode *node = data->nodes[i];
  DirtyMaskRangeData *range = tls->userdata_chunk;
  PBVHVertexIter vd;

  BKE_pbvh_vertex_iter_begin (ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
    float dirty_mask = neighbor_dirty_mask(ss, &vd);
    range->min = min_ff(dirty_mask, range->min);
    range->max = max_ff(dirty_mask, range->max);
  }
  BKE_pbvh_vertex_iter_end;
}

static void dirty_mask_compute_range_reduce(const void *__restrict UNUSED(userdata),
                                            void *__restrict chunk_join,
                                            void *__restrict chunk)
{
  DirtyMaskRangeData *join = chunk_join;
  DirtyMaskRangeData *range = chunk;
  join->min = min_ff(range->min, join->min);
  join->max = max_ff(range->max, join->max);
}

static void dirty_mask_apply_task_cb(void *__restrict userdata,
                                     const int i,
                                     const TaskParallelTLS *__restrict UNUSED(tls))
{
  SculptThreadedTaskData *data = userdata;
  SculptSession *ss = data->ob->sculpt;
  PBVHNode *node = data->nodes[i];
  PBVHVertexIter vd;

  const bool dirty_only = data->dirty_mask_dirty_only;
  const float min = data->dirty_mask_min;
  const float max = data->dirty_mask_max;

  float range = max - min;
  if (range < 0.0001f) {
    range = 0.0f;
  }
  else {
    range = 1.0f / range;
  }

  BKE_pbvh_vertex_iter_begin (ss->pbvh, node, vd, PBVH_ITER_UNIQUE) {
    float dirty_mask = neighbor_dirty_mask(ss, &vd);
    float mask = *vd.mask + (1.0f - ((dirty_mask - min) * range));
    if (dirty_only) {
      mask = fminf(mask, 0.5f) * 2.0f;
    }
    *vd.mask = CLAMPIS(mask, 0.0f, 1.0f);

    if (vd.mvert) {
      vd.mvert->flag |= ME_VERT_PBVH_UPDATE;
    }
  }
  BKE_pbvh_vertex_iter_end;
  BKE_pbvh_node_mark_update_mask(node);
}

static int sculpt_dirty_mask_exec(bContext *C, wmOperator *op)
{
  ARegion *region = CTX_wm_region(C);
  Object *ob = CTX_data_active_object(C);
  SculptSession *ss = ob->sculpt;
  Depsgraph *depsgraph = CTX_data_depsgraph_pointer(C);
  PBVH *pbvh = ob->sculpt->pbvh;
  PBVHNode **nodes;
  Sculpt *sd = CTX_data_tool_settings(C)->sculpt;
  int totnode;

  BKE_sculpt_update_object_for_edit(depsgraph, ob, true, true, false);

  SCULPT_vertex_random_access_ensure(ss);

  if (!ob->sculpt->pmap) {
    return OPERATOR_CANCELLED;
  }

  BKE_pbvh_search_gather(pbvh, NULL, NULL, &nodes, &totnode);
  SCULPT_undo_push_begin(ob, "Dirty Mask");

  for (int i = 0; i < totnode; i++) {
    SCULPT_undo_push_node(ob, nodes[i], SCULPT_UNDO_MASK);
  }

  SculptThreadedTaskData data = {
      .sd = sd,
      .ob = ob,
      .nodes = nodes,
      .dirty_mask_dirty_only = RNA_boolean_get(op->ptr, "dirty_only"),
  };
  DirtyMaskRangeData range = {
      .min = FLT_MAX,
      .max = -FLT_MAX,
  };

  TaskParallelSettings settings;
  BKE_pbvh_parallel_range_settings(&settings, true, totnode);

  settings.func_reduce = dirty_mask_compute_range_reduce;
  settings.userdata_chunk = &range;
  settings.userdata_chunk_size = sizeof(DirtyMaskRangeData);

  BLI_task_parallel_range(0, totnode, &data, dirty_mask_compute_range_task_cb, &settings);
  data.dirty_mask_min = range.min;
  data.dirty_mask_max = range.max;
  BLI_task_parallel_range(0, totnode, &data, dirty_mask_apply_task_cb, &settings);

  MEM_SAFE_FREE(nodes);

  BKE_pbvh_update_vertex_data(pbvh, PBVH_UpdateMask);

  SCULPT_undo_push_end();

  ED_region_tag_redraw(region);

  WM_event_add_notifier(C, NC_OBJECT | ND_DRAW, ob);

  return OPERATOR_FINISHED;
}

void SCULPT_OT_dirty_mask(struct wmOperatorType *ot)
{
  /* Identifiers. */
  ot->name = "Dirty Mask";
  ot->idname = "SCULPT_OT_dirty_mask";
  ot->description = "Generates a mask based on the geometry cavity and pointiness";

  /* API callbacks. */
  ot->exec = sculpt_dirty_mask_exec;
  ot->poll = SCULPT_mode_poll;

  ot->flag = OPTYPE_REGISTER;

  /* RNA. */
  RNA_def_boolean(
      ot->srna, "dirty_only", false, "Dirty Only", "Don't calculate cleans for convex areas");
}