1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License
4  * as published by the Free Software Foundation; either version 2
5  * of the License, or (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10  * GNU General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software Foundation,
14  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
15  *
16  * The Original Code is Copyright (C) 2006 by Nicholas Bishop
17  * All rights reserved.
18  */
19 
20 /** \file
21  * \ingroup edsculpt
22  */
23 
24 #pragma once
25 
26 #include "DNA_brush_types.h"
27 #include "DNA_key_types.h"
28 #include "DNA_listBase.h"
29 #include "DNA_meshdata_types.h"
30 #include "DNA_vec_types.h"
31 
32 #include "BLI_bitmap.h"
33 #include "BLI_gsqueue.h"
34 #include "BLI_threads.h"
35 
36 #include "BKE_paint.h"
37 #include "BKE_pbvh.h"
38 
39 struct AutomaskingCache;
40 struct KeyBlock;
41 struct Object;
42 struct SculptPoseIKChainSegment;
43 struct SculptUndoNode;
44 struct bContext;
45 
46 enum ePaintSymmetryFlags;
47 
48 bool SCULPT_mode_poll(struct bContext *C);
49 bool SCULPT_mode_poll_view3d(struct bContext *C);
50 /* checks for a brush, not just sculpt mode */
51 bool SCULPT_poll(struct bContext *C);
52 bool SCULPT_poll_view3d(struct bContext *C);
53 
54 bool SCULPT_vertex_colors_poll(struct bContext *C);
55 
56 /* Updates */
57 
58 typedef enum SculptUpdateType {
59   SCULPT_UPDATE_COORDS = 1 << 0,
60   SCULPT_UPDATE_MASK = 1 << 1,
61   SCULPT_UPDATE_VISIBILITY = 1 << 2,
62   SCULPT_UPDATE_COLOR = 1 << 3,
63 } SculptUpdateType;
64 
65 void SCULPT_flush_update_step(bContext *C, SculptUpdateType update_flags);
66 void SCULPT_flush_update_done(const bContext *C, Object *ob, SculptUpdateType update_flags);
67 void SCULPT_flush_stroke_deform(struct Sculpt *sd, Object *ob, bool is_proxy_used);
68 
69 /* Should be used after modifying the mask or Face Sets IDs. */
70 void SCULPT_tag_update_overlays(bContext *C);
71 
72 /* Stroke */
73 
74 typedef struct SculptCursorGeometryInfo {
75   float location[3];
76   float normal[3];
77   float active_vertex_co[3];
78 } SculptCursorGeometryInfo;
79 
80 bool SCULPT_stroke_get_location(struct bContext *C, float out[3], const float mouse[2]);
81 bool SCULPT_cursor_geometry_info_update(bContext *C,
82                                         SculptCursorGeometryInfo *out,
83                                         const float mouse[2],
84                                         bool use_sampled_normal);
85 void SCULPT_geometry_preview_lines_update(bContext *C, struct SculptSession *ss, float radius);
86 
87 void SCULPT_stroke_modifiers_check(const bContext *C, Object *ob, const Brush *brush);
88 float SCULPT_raycast_init(struct ViewContext *vc,
89                           const float mouse[2],
90                           float ray_start[3],
91                           float ray_end[3],
92                           float ray_normal[3],
93                           bool original);
94 
95 /* Symmetry */
96 char SCULPT_mesh_symmetry_xyz_get(Object *object);
97 
98 /* Sculpt PBVH abstraction API */
99 void SCULPT_vertex_random_access_ensure(struct SculptSession *ss);
100 
101 int SCULPT_vertex_count_get(struct SculptSession *ss);
102 const float *SCULPT_vertex_co_get(struct SculptSession *ss, int index);
103 void SCULPT_vertex_normal_get(SculptSession *ss, int index, float no[3]);
104 float SCULPT_vertex_mask_get(struct SculptSession *ss, int index);
105 const float *SCULPT_vertex_color_get(SculptSession *ss, int index);
106 
107 const float *SCULPT_vertex_persistent_co_get(SculptSession *ss, int index);
108 void SCULPT_vertex_persistent_normal_get(SculptSession *ss, int index, float no[3]);
109 
110 /* Coordinates used for manipulating the base mesh when Grab Active Vertex is enabled. */
111 const float *SCULPT_vertex_co_for_grab_active_get(SculptSession *ss, int index);
112 
113 /* Returns the info of the limit surface when Multires is available, otherwise it returns the
114  * current coordinate of the vertex. */
115 void SCULPT_vertex_limit_surface_get(SculptSession *ss, int index, float r_co[3]);
116 
117 /* Returns the pointer to the coordinates that should be edited from a brush tool iterator
118  * depending on the given deformation target. */
119 float *SCULPT_brush_deform_target_vertex_co_get(SculptSession *ss,
120                                                 const int deform_target,
121                                                 PBVHVertexIter *iter);
122 
123 #define SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY 256
124 typedef struct SculptVertexNeighborIter {
125   /* Storage */
126   int *neighbors;
127   int size;
128   int capacity;
129   int neighbors_fixed[SCULPT_VERTEX_NEIGHBOR_FIXED_CAPACITY];
130 
131   /* Internal iterator. */
132   int num_duplicates;
133   int i;
134 
135   /* Public */
136   int index;
137   bool is_duplicate;
138 } SculptVertexNeighborIter;
139 
140 void SCULPT_vertex_neighbors_get(struct SculptSession *ss,
141                                  const int index,
142                                  const bool include_duplicates,
143                                  SculptVertexNeighborIter *iter);
144 
145 /* Iterator over neighboring vertices. */
146 #define SCULPT_VERTEX_NEIGHBORS_ITER_BEGIN(ss, v_index, neighbor_iterator) \
147   SCULPT_vertex_neighbors_get(ss, v_index, false, &neighbor_iterator); \
148   for (neighbor_iterator.i = 0; neighbor_iterator.i < neighbor_iterator.size; \
149        neighbor_iterator.i++) { \
150     neighbor_iterator.index = neighbor_iterator.neighbors[neighbor_iterator.i];
151 
152 /* Iterate over neighboring and duplicate vertices (for PBVH_GRIDS). Duplicates come
153  * first since they are nearest for floodfill. */
154 #define SCULPT_VERTEX_DUPLICATES_AND_NEIGHBORS_ITER_BEGIN(ss, v_index, neighbor_iterator) \
155   SCULPT_vertex_neighbors_get(ss, v_index, true, &neighbor_iterator); \
156   for (neighbor_iterator.i = neighbor_iterator.size - 1; neighbor_iterator.i >= 0; \
157        neighbor_iterator.i--) { \
158     neighbor_iterator.index = neighbor_iterator.neighbors[neighbor_iterator.i]; \
159     neighbor_iterator.is_duplicate = (neighbor_iterator.i >= \
160                                       neighbor_iterator.size - neighbor_iterator.num_duplicates);
161 
162 #define SCULPT_VERTEX_NEIGHBORS_ITER_END(neighbor_iterator) \
163   } \
164   if (neighbor_iterator.neighbors != neighbor_iterator.neighbors_fixed) { \
165     MEM_freeN(neighbor_iterator.neighbors); \
166   } \
167   ((void)0)
168 
169 int SCULPT_active_vertex_get(SculptSession *ss);
170 const float *SCULPT_active_vertex_co_get(SculptSession *ss);
171 void SCULPT_active_vertex_normal_get(SculptSession *ss, float normal[3]);
172 
173 /* Returns PBVH deformed vertices array if shape keys or deform modifiers are used, otherwise
174  * returns mesh original vertices array. */
175 struct MVert *SCULPT_mesh_deformed_mverts_get(SculptSession *ss);
176 
177 /* Fake Neighbors */
178 
179 #define FAKE_NEIGHBOR_NONE -1
180 
181 void SCULPT_fake_neighbors_ensure(struct Sculpt *sd, Object *ob, const float max_dist);
182 void SCULPT_fake_neighbors_enable(Object *ob);
183 void SCULPT_fake_neighbors_disable(Object *ob);
184 void SCULPT_fake_neighbors_free(struct Object *ob);
185 
186 /* Vertex Info. */
187 void SCULPT_boundary_info_ensure(Object *object);
188 /* Boundary Info needs to be initialized in order to use this function. */
189 bool SCULPT_vertex_is_boundary(const SculptSession *ss, const int index);
190 
191 /* Sculpt Visibility API */
192 
193 void SCULPT_vertex_visible_set(SculptSession *ss, int index, bool visible);
194 bool SCULPT_vertex_visible_get(SculptSession *ss, int index);
195 
196 void SCULPT_visibility_sync_all_face_sets_to_vertices(struct Object *ob);
197 void SCULPT_visibility_sync_all_vertex_to_face_sets(struct SculptSession *ss);
198 
199 /* Face Sets API */
200 
201 int SCULPT_active_face_set_get(SculptSession *ss);
202 int SCULPT_vertex_face_set_get(SculptSession *ss, int index);
203 void SCULPT_vertex_face_set_set(SculptSession *ss, int index, int face_set);
204 
205 bool SCULPT_vertex_has_face_set(SculptSession *ss, int index, int face_set);
206 bool SCULPT_vertex_has_unique_face_set(SculptSession *ss, int index);
207 
208 int SCULPT_face_set_next_available_get(SculptSession *ss);
209 
210 void SCULPT_face_set_visibility_set(SculptSession *ss, int face_set, bool visible);
211 bool SCULPT_vertex_all_face_sets_visible_get(const SculptSession *ss, int index);
212 bool SCULPT_vertex_any_face_set_visible_get(SculptSession *ss, int index);
213 
214 void SCULPT_face_sets_visibility_invert(SculptSession *ss);
215 void SCULPT_face_sets_visibility_all_set(SculptSession *ss, bool visible);
216 
217 bool SCULPT_stroke_is_main_symmetry_pass(struct StrokeCache *cache);
218 bool SCULPT_stroke_is_first_brush_step(struct StrokeCache *cache);
219 bool SCULPT_stroke_is_first_brush_step_of_symmetry_pass(struct StrokeCache *cache);
220 
221 /* Sculpt Original Data */
222 typedef struct {
223   struct BMLog *bm_log;
224 
225   struct SculptUndoNode *unode;
226   float (*coords)[3];
227   short (*normals)[3];
228   const float *vmasks;
229   float (*colors)[4];
230 
231   /* Original coordinate, normal, and mask. */
232   const float *co;
233   const short *no;
234   float mask;
235   const float *col;
236 } SculptOrigVertData;
237 
238 void SCULPT_orig_vert_data_init(SculptOrigVertData *data, Object *ob, PBVHNode *node);
239 void SCULPT_orig_vert_data_update(SculptOrigVertData *orig_data, PBVHVertexIter *iter);
240 void SCULPT_orig_vert_data_unode_init(SculptOrigVertData *data,
241                                       Object *ob,
242                                       struct SculptUndoNode *unode);
243 
244 /* Utils. */
245 void SCULPT_calc_brush_plane(struct Sculpt *sd,
246                              struct Object *ob,
247                              struct PBVHNode **nodes,
248                              int totnode,
249                              float r_area_no[3],
250                              float r_area_co[3]);
251 
252 void SCULPT_calc_area_normal(
253     Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode, float r_area_no[3]);
254 
255 int SCULPT_nearest_vertex_get(struct Sculpt *sd,
256                               struct Object *ob,
257                               const float co[3],
258                               float max_distance,
259                               bool use_original);
260 
261 int SCULPT_plane_point_side(const float co[3], const float plane[4]);
262 int SCULPT_plane_trim(const struct StrokeCache *cache,
263                       const struct Brush *brush,
264                       const float val[3]);
265 void SCULPT_clip(Sculpt *sd, SculptSession *ss, float co[3], const float val[3]);
266 
267 float SCULPT_brush_plane_offset_get(Sculpt *sd, SculptSession *ss);
268 
269 ePaintSymmetryAreas SCULPT_get_vertex_symm_area(const float co[3]);
270 bool SCULPT_check_vertex_pivot_symmetry(const float vco[3], const float pco[3], const char symm);
271 bool SCULPT_is_vertex_inside_brush_radius_symm(const float vertex[3],
272                                                const float br_co[3],
273                                                float radius,
274                                                char symm);
275 bool SCULPT_is_symmetry_iteration_valid(char i, char symm);
276 void SCULPT_flip_v3_by_symm_area(float v[3],
277                                  const ePaintSymmetryFlags symm,
278                                  const ePaintSymmetryAreas symmarea,
279                                  const float pivot[3]);
280 void SCULPT_flip_quat_by_symm_area(float quat[3],
281                                    const ePaintSymmetryFlags symm,
282                                    const ePaintSymmetryAreas symmarea,
283                                    const float pivot[3]);
284 
285 /* Flood Fill. */
286 typedef struct {
287   GSQueue *queue;
288   BLI_bitmap *visited_vertices;
289 } SculptFloodFill;
290 
291 void SCULPT_floodfill_init(struct SculptSession *ss, SculptFloodFill *flood);
292 void SCULPT_floodfill_add_active(struct Sculpt *sd,
293                                  struct Object *ob,
294                                  struct SculptSession *ss,
295                                  SculptFloodFill *flood,
296                                  float radius);
297 void SCULPT_floodfill_add_initial_with_symmetry(struct Sculpt *sd,
298                                                 struct Object *ob,
299                                                 struct SculptSession *ss,
300                                                 SculptFloodFill *flood,
301                                                 int index,
302                                                 float radius);
303 void SCULPT_floodfill_add_initial(SculptFloodFill *flood, int index);
304 void SCULPT_floodfill_execute(
305     struct SculptSession *ss,
306     SculptFloodFill *flood,
307     bool (*func)(SculptSession *ss, int from_v, int to_v, bool is_duplicate, void *userdata),
308     void *userdata);
309 void SCULPT_floodfill_free(SculptFloodFill *flood);
310 
311 /* Dynamic topology */
312 
313 enum eDynTopoWarnFlag {
314   DYNTOPO_WARN_VDATA = (1 << 0),
315   DYNTOPO_WARN_EDATA = (1 << 1),
316   DYNTOPO_WARN_LDATA = (1 << 2),
317   DYNTOPO_WARN_MODIFIER = (1 << 3),
318 };
319 
320 void SCULPT_dynamic_topology_enable_ex(struct Main *bmain,
321                                        struct Depsgraph *depsgraph,
322                                        Scene *scene,
323                                        Object *ob);
324 void SCULPT_dynamic_topology_disable(bContext *C, struct SculptUndoNode *unode);
325 void sculpt_dynamic_topology_disable_with_undo(struct Main *bmain,
326                                                struct Depsgraph *depsgraph,
327                                                Scene *scene,
328                                                Object *ob);
329 
330 bool SCULPT_stroke_is_dynamic_topology(const SculptSession *ss, const Brush *brush);
331 
332 void SCULPT_dynamic_topology_triangulate(struct BMesh *bm);
333 void SCULPT_dyntopo_node_layers_add(struct SculptSession *ss);
334 
335 enum eDynTopoWarnFlag SCULPT_dynamic_topology_check(Scene *scene, Object *ob);
336 
337 void SCULPT_pbvh_clear(Object *ob);
338 
339 /* Auto-masking. */
340 float SCULPT_automasking_factor_get(struct AutomaskingCache *automasking,
341                                     SculptSession *ss,
342                                     int vert);
343 
344 /* Returns the automasking cache depending on the active tool. Used for code that can run both for
345  * brushes and filter. */
346 struct AutomaskingCache *SCULPT_automasking_active_cache_get(SculptSession *ss);
347 
348 struct AutomaskingCache *SCULPT_automasking_cache_init(Sculpt *sd, Brush *brush, Object *ob);
349 void SCULPT_automasking_cache_free(struct AutomaskingCache *automasking);
350 
351 bool SCULPT_is_automasking_mode_enabled(const Sculpt *sd,
352                                         const Brush *br,
353                                         const eAutomasking_flag mode);
354 bool SCULPT_is_automasking_enabled(const Sculpt *sd, const SculptSession *ss, const Brush *br);
355 
356 typedef enum eBoundaryAutomaskMode {
357   AUTOMASK_INIT_BOUNDARY_EDGES = 1,
358   AUTOMASK_INIT_BOUNDARY_FACE_SETS = 2,
359 } eBoundaryAutomaskMode;
360 float *SCULPT_boundary_automasking_init(Object *ob,
361                                         eBoundaryAutomaskMode mode,
362                                         int propagation_steps,
363                                         float *automask_factor);
364 
365 /* Filters. */
366 void SCULPT_filter_cache_init(struct bContext *C, Object *ob, Sculpt *sd, const int undo_type);
367 void SCULPT_filter_cache_free(SculptSession *ss);
368 
369 void SCULPT_mask_filter_smooth_apply(
370     Sculpt *sd, Object *ob, PBVHNode **nodes, const int totnode, const int smooth_iterations);
371 
372 /* Brushes. */
373 
374 /* Cloth Brush. */
375 void SCULPT_do_cloth_brush(struct Sculpt *sd,
376                            struct Object *ob,
377                            struct PBVHNode **nodes,
378                            int totnode);
379 
380 void SCULPT_cloth_simulation_free(struct SculptClothSimulation *cloth_sim);
381 
382 struct SculptClothSimulation *SCULPT_cloth_brush_simulation_create(
383     struct SculptSession *ss,
384     const float cloth_mass,
385     const float cloth_damping,
386     const float cloth_softbody_strength,
387     const bool use_collisions,
388     const bool needs_deform_coords);
389 void SCULPT_cloth_brush_simulation_init(struct SculptSession *ss,
390                                         struct SculptClothSimulation *cloth_sim);
391 
392 void SCULPT_cloth_sim_activate_nodes(struct SculptClothSimulation *cloth_sim,
393                                      PBVHNode **nodes,
394                                      int totnode);
395 
396 void SCULPT_cloth_brush_store_simulation_state(struct SculptSession *ss,
397                                                struct SculptClothSimulation *cloth_sim);
398 
399 void SCULPT_cloth_brush_do_simulation_step(struct Sculpt *sd,
400                                            struct Object *ob,
401                                            struct SculptClothSimulation *cloth_sim,
402                                            struct PBVHNode **nodes,
403                                            int totnode);
404 
405 void SCULPT_cloth_brush_ensure_nodes_constraints(struct Sculpt *sd,
406                                                  struct Object *ob,
407                                                  struct PBVHNode **nodes,
408                                                  int totnode,
409                                                  struct SculptClothSimulation *cloth_sim,
410                                                  float initial_location[3],
411                                                  const float radius);
412 
413 void SCULPT_cloth_simulation_limits_draw(const uint gpuattr,
414                                          const struct Brush *brush,
415                                          const float location[3],
416                                          const float normal[3],
417                                          const float rds,
418                                          const float line_width,
419                                          const float outline_col[3],
420                                          const float alpha);
421 void SCULPT_cloth_plane_falloff_preview_draw(const uint gpuattr,
422                                              struct SculptSession *ss,
423                                              const float outline_col[3],
424                                              float outline_alpha);
425 
SCULPT_is_cloth_deform_brush(const Brush * brush)426 BLI_INLINE bool SCULPT_is_cloth_deform_brush(const Brush *brush)
427 {
428   return (brush->sculpt_tool == SCULPT_TOOL_CLOTH && ELEM(brush->cloth_deform_type,
429                                                           BRUSH_CLOTH_DEFORM_GRAB,
430                                                           BRUSH_CLOTH_DEFORM_SNAKE_HOOK)) ||
431          /* All brushes that are not the cloth brush deform the simulation using softbody
432           * constraints instead of applying forces. */
433          (brush->sculpt_tool != SCULPT_TOOL_CLOTH &&
434           brush->deform_target == BRUSH_DEFORM_TARGET_CLOTH_SIM);
435 }
436 
437 /* Pose Brush. */
438 void SCULPT_do_pose_brush(struct Sculpt *sd,
439                           struct Object *ob,
440                           struct PBVHNode **nodes,
441                           int totnode);
442 void SCULPT_pose_calc_pose_data(struct Sculpt *sd,
443                                 struct Object *ob,
444                                 struct SculptSession *ss,
445                                 float initial_location[3],
446                                 float radius,
447                                 float pose_offset,
448                                 float *r_pose_origin,
449                                 float *r_pose_factor);
450 void SCULPT_pose_brush_init(struct Sculpt *sd,
451                             struct Object *ob,
452                             struct SculptSession *ss,
453                             struct Brush *br);
454 struct SculptPoseIKChain *SCULPT_pose_ik_chain_init(struct Sculpt *sd,
455                                                     struct Object *ob,
456                                                     struct SculptSession *ss,
457                                                     struct Brush *br,
458                                                     const float initial_location[3],
459                                                     const float radius);
460 void SCULPT_pose_ik_chain_free(struct SculptPoseIKChain *ik_chain);
461 
462 /* Boundary Brush. */
463 struct SculptBoundary *SCULPT_boundary_data_init(Object *object,
464                                                  Brush *brush,
465                                                  const int initial_vertex,
466                                                  const float radius);
467 void SCULPT_boundary_data_free(struct SculptBoundary *boundary);
468 void SCULPT_do_boundary_brush(struct Sculpt *sd,
469                               struct Object *ob,
470                               struct PBVHNode **nodes,
471                               int totnode);
472 
473 void SCULPT_boundary_edges_preview_draw(const uint gpuattr,
474                                         struct SculptSession *ss,
475                                         const float outline_col[3],
476                                         const float outline_alpha);
477 void SCULPT_boundary_pivot_line_preview_draw(const uint gpuattr, struct SculptSession *ss);
478 
479 /* Multiplane Scrape Brush. */
480 void SCULPT_do_multiplane_scrape_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
481 void SCULPT_multiplane_scrape_preview_draw(const uint gpuattr,
482                                            Brush *brush,
483                                            SculptSession *ss,
484                                            const float outline_col[3],
485                                            const float outline_alpha);
486 /* Draw Face Sets Brush. */
487 void SCULPT_do_draw_face_sets_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
488 
489 /* Paint Brush. */
490 void SCULPT_do_paint_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
491 
492 /* Smear Brush. */
493 void SCULPT_do_smear_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
494 
495 /* Smooth Brush. */
496 void SCULPT_bmesh_four_neighbor_average(float avg[3], float direction[3], struct BMVert *v);
497 
498 void SCULPT_neighbor_coords_average(SculptSession *ss, float result[3], int index);
499 float SCULPT_neighbor_mask_average(SculptSession *ss, int index);
500 void SCULPT_neighbor_color_average(SculptSession *ss, float result[4], int index);
501 
502 /* Mask the mesh boundaries smoothing only the mesh surface without using automasking. */
503 void SCULPT_neighbor_coords_average_interior(SculptSession *ss, float result[3], int index);
504 
505 void SCULPT_smooth(Sculpt *sd,
506                    Object *ob,
507                    PBVHNode **nodes,
508                    const int totnode,
509                    float bstrength,
510                    const bool smooth_mask);
511 void SCULPT_do_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
512 
513 /* Surface Smooth Brush. */
514 
515 void SCULPT_surface_smooth_laplacian_step(SculptSession *ss,
516                                           float *disp,
517                                           const float co[3],
518                                           float (*laplacian_disp)[3],
519                                           const int v_index,
520                                           const float origco[3],
521                                           const float alpha);
522 void SCULPT_surface_smooth_displace_step(SculptSession *ss,
523                                          float *co,
524                                          float (*laplacian_disp)[3],
525                                          const int v_index,
526                                          const float beta,
527                                          const float fade);
528 void SCULPT_do_surface_smooth_brush(Sculpt *sd, Object *ob, PBVHNode **nodes, int totnode);
529 
530 /* Slide/Relax */
531 void SCULPT_relax_vertex(struct SculptSession *ss,
532                          struct PBVHVertexIter *vd,
533                          float factor,
534                          bool filter_boundary_face_sets,
535                          float *r_final_pos);
536 
537 /* Undo */
538 
539 typedef enum {
540   SCULPT_UNDO_COORDS,
541   SCULPT_UNDO_HIDDEN,
542   SCULPT_UNDO_MASK,
543   SCULPT_UNDO_DYNTOPO_BEGIN,
544   SCULPT_UNDO_DYNTOPO_END,
545   SCULPT_UNDO_DYNTOPO_SYMMETRIZE,
546   SCULPT_UNDO_GEOMETRY,
547   SCULPT_UNDO_FACE_SETS,
548   SCULPT_UNDO_COLOR,
549 } SculptUndoType;
550 
551 /* Storage of geometry for the undo node.
552  * Is used as a storage for either original or modified geometry. */
553 typedef struct SculptUndoNodeGeometry {
554   /* Is used for sanity check, helping with ensuring that two and only two
555    * geometry pushes happened in the undo stack. */
556   bool is_initialized;
557 
558   CustomData vdata;
559   CustomData edata;
560   CustomData ldata;
561   CustomData pdata;
562   int totvert;
563   int totedge;
564   int totloop;
565   int totpoly;
566 } SculptUndoNodeGeometry;
567 
568 typedef struct SculptUndoNode {
569   struct SculptUndoNode *next, *prev;
570 
571   SculptUndoType type;
572 
573   char idname[MAX_ID_NAME]; /* name instead of pointer*/
574   void *node;               /* only during push, not valid afterwards! */
575 
576   float (*co)[3];
577   float (*orig_co)[3];
578   short (*no)[3];
579   float (*col)[4];
580   float *mask;
581   int totvert;
582 
583   /* non-multires */
584   int maxvert; /* to verify if totvert it still the same */
585   int *index;  /* to restore into right location */
586   BLI_bitmap *vert_hidden;
587 
588   /* multires */
589   int maxgrid;  /* same for grid */
590   int gridsize; /* same for grid */
591   int totgrid;  /* to restore into right location */
592   int *grids;   /* to restore into right location */
593   BLI_bitmap **grid_hidden;
594 
595   /* bmesh */
596   struct BMLogEntry *bm_entry;
597   bool applied;
598 
599   /* shape keys */
600   char shapeName[sizeof(((KeyBlock *)0))->name];
601 
602   /* Geometry modification operations.
603    *
604    * Original geometry is stored before some modification is run and is used to restore state of
605    * the object when undoing the operation
606    *
607    * Modified geometry is stored after the modification and is used to redo the modification. */
608   bool geometry_clear_pbvh;
609   SculptUndoNodeGeometry geometry_original;
610   SculptUndoNodeGeometry geometry_modified;
611 
612   /* Geometry at the bmesh enter moment. */
613   SculptUndoNodeGeometry geometry_bmesh_enter;
614 
615   /* pivot */
616   float pivot_pos[3];
617   float pivot_rot[4];
618 
619   /* Sculpt Face Sets */
620   int *face_sets;
621 
622   size_t undo_size;
623 } SculptUndoNode;
624 
625 /* Factor of brush to have rake point following behind
626  * (could be configurable but this is reasonable default). */
627 #define SCULPT_RAKE_BRUSH_FACTOR 0.25f
628 
629 struct SculptRakeData {
630   float follow_dist;
631   float follow_co[3];
632 };
633 
634 /* Single struct used by all BLI_task threaded callbacks, let's avoid adding 10's of those... */
635 typedef struct SculptThreadedTaskData {
636   struct bContext *C;
637   struct Sculpt *sd;
638   struct Object *ob;
639   const struct Brush *brush;
640   struct PBVHNode **nodes;
641   int totnode;
642 
643   struct VPaint *vp;
644   struct VPaintData *vpd;
645   struct WPaintData *wpd;
646   struct WeightPaintInfo *wpi;
647   unsigned int *lcol;
648   struct Mesh *me;
649   /* For passing generic params. */
650   void *custom_data;
651 
652   /* Data specific to some callbacks. */
653 
654   /* Note: even if only one or two of those are used at a time,
655    *       keeping them separated, names help figuring out
656    *       what it is, and memory overhead is ridiculous anyway. */
657   float flippedbstrength;
658   float angle;
659   float strength;
660   bool smooth_mask;
661   bool has_bm_orco;
662 
663   struct SculptProjectVector *spvc;
664   float *offset;
665   float *grab_delta;
666   float *cono;
667   float *area_no;
668   float *area_no_sp;
669   float *area_co;
670   float (*mat)[4];
671   float (*vertCos)[3];
672 
673   /* X and Z vectors aligned to the stroke direction for operations where perpendicular vectors to
674    * the stroke direction are needed. */
675   float (*stroke_xz)[3];
676 
677   int filter_type;
678   float filter_strength;
679   float *filter_fill_color;
680 
681   bool use_area_cos;
682   bool use_area_nos;
683 
684   /* 0=towards view, 1=flipped */
685   float (*area_cos)[3];
686   float (*area_nos)[3];
687   int *count_no;
688   int *count_co;
689 
690   bool any_vertex_sampled;
691 
692   float *wet_mix_sampled_color;
693 
694   float *prev_mask;
695 
696   float *pose_factor;
697   float *pose_initial_co;
698   int pose_chain_segment;
699 
700   float multiplane_scrape_angle;
701   float multiplane_scrape_planes[2][4];
702 
703   float max_distance_squared;
704   float nearest_vertex_search_co[3];
705 
706   /* Stabilized strength for the Clay Thumb brush. */
707   float clay_strength;
708 
709   int mask_expand_update_it;
710   bool mask_expand_invert_mask;
711   bool mask_expand_use_normals;
712   bool mask_expand_keep_prev_mask;
713   bool mask_expand_create_face_set;
714 
715   float transform_mats[8][4][4];
716 
717   /* Boundary brush */
718   float boundary_deform_strength;
719 
720   float cloth_time_step;
721   SculptClothSimulation *cloth_sim;
722   float *cloth_sim_initial_location;
723   float cloth_sim_radius;
724 
725   float dirty_mask_min;
726   float dirty_mask_max;
727   bool dirty_mask_dirty_only;
728 
729   /* Mask By Color Tool */
730 
731   float mask_by_color_threshold;
732   bool mask_by_color_invert;
733   bool mask_by_color_preserve_mask;
734 
735   /* Index of the vertex that is going to be used as a reference for the colors. */
736   int mask_by_color_vertex;
737   float *mask_by_color_floodfill;
738 
739   int face_set;
740   int filter_undo_type;
741 
742   ThreadMutex mutex;
743 
744 } SculptThreadedTaskData;
745 
746 /*************** Brush testing declarations ****************/
747 typedef struct SculptBrushTest {
748   float radius_squared;
749   float radius;
750   float location[3];
751   float dist;
752   int mirror_symmetry_pass;
753 
754   int radial_symmetry_pass;
755   float symm_rot_mat_inv[4][4];
756 
757   /* For circle (not sphere) projection. */
758   float plane_view[4];
759 
760   /* Some tool code uses a plane for its calculations. */
761   float plane_tool[4];
762 
763   /* View3d clipping - only set rv3d for clipping */
764   struct RegionView3D *clip_rv3d;
765 } SculptBrushTest;
766 
767 typedef bool (*SculptBrushTestFn)(SculptBrushTest *test, const float co[3]);
768 
769 typedef struct {
770   struct Sculpt *sd;
771   struct SculptSession *ss;
772   float radius_squared;
773   const float *center;
774   bool original;
775   /* This ignores fully masked and fully hidden nodes. */
776   bool ignore_fully_ineffective;
777 } SculptSearchSphereData;
778 
779 typedef struct {
780   struct Sculpt *sd;
781   struct SculptSession *ss;
782   float radius_squared;
783   bool original;
784   bool ignore_fully_ineffective;
785   struct DistRayAABB_Precalc *dist_ray_to_aabb_precalc;
786 } SculptSearchCircleData;
787 
788 void SCULPT_brush_test_init(struct SculptSession *ss, SculptBrushTest *test);
789 bool SCULPT_brush_test_sphere(SculptBrushTest *test, const float co[3]);
790 bool SCULPT_brush_test_sphere_sq(SculptBrushTest *test, const float co[3]);
791 bool SCULPT_brush_test_sphere_fast(const SculptBrushTest *test, const float co[3]);
792 bool SCULPT_brush_test_cube(SculptBrushTest *test,
793                             const float co[3],
794                             const float local[4][4],
795                             const float roundness);
796 bool SCULPT_brush_test_circle_sq(SculptBrushTest *test, const float co[3]);
797 bool SCULPT_search_sphere_cb(PBVHNode *node, void *data_v);
798 bool SCULPT_search_circle_cb(PBVHNode *node, void *data_v);
799 
800 SculptBrushTestFn SCULPT_brush_test_init_with_falloff_shape(SculptSession *ss,
801                                                             SculptBrushTest *test,
802                                                             char falloff_shape);
803 const float *SCULPT_brush_frontface_normal_from_falloff_shape(SculptSession *ss,
804                                                               char falloff_shape);
805 
806 float SCULPT_brush_strength_factor(struct SculptSession *ss,
807                                    const struct Brush *br,
808                                    const float point[3],
809                                    const float len,
810                                    const short vno[3],
811                                    const float fno[3],
812                                    const float mask,
813                                    const int vertex_index,
814                                    const int thread_id);
815 
816 /* Tilts a normal by the x and y tilt values using the view axis. */
817 void SCULPT_tilt_apply_to_normal(float r_normal[3],
818                                  struct StrokeCache *cache,
819                                  const float tilt_strength);
820 
821 /* Get effective surface normal with pen tilt and tilt strength applied to it. */
822 void SCULPT_tilt_effective_normal_get(const SculptSession *ss, const Brush *brush, float r_no[3]);
823 
824 /* just for vertex paint. */
825 bool SCULPT_pbvh_calc_area_normal(const struct Brush *brush,
826                                   Object *ob,
827                                   PBVHNode **nodes,
828                                   int totnode,
829                                   bool use_threading,
830                                   float r_area_no[3]);
831 
832 /* Cache stroke properties. Used because
833  * RNA property lookup isn't particularly fast.
834  *
835  * For descriptions of these settings, check the operator properties.
836  */
837 
838 #define SCULPT_CLAY_STABILIZER_LEN 10
839 
840 typedef struct AutomaskingSettings {
841   /* Flags from eAutomasking_flag. */
842   int flags;
843   int initial_face_set;
844 } AutomaskingSettings;
845 
846 typedef struct AutomaskingCache {
847   AutomaskingSettings settings;
848   /* Precomputed auto-mask factor indexed by vertex, owned by the auto-masking system and
849    * initialized in #SCULPT_automasking_cache_init when needed. */
850   float *factor;
851 } AutomaskingCache;
852 
853 typedef struct StrokeCache {
854   /* Invariants */
855   float initial_radius;
856   float scale[3];
857   int flag;
858   float clip_tolerance[3];
859   float initial_mouse[2];
860 
861   /* Variants */
862   float radius;
863   float radius_squared;
864   float true_location[3];
865   float true_last_location[3];
866   float location[3];
867   float last_location[3];
868 
869   /* Used for alternating between deformation in brushes that need to apply different ones to
870    * achieve certain effects. */
871   int iteration_count;
872 
873   /* Original pixel radius with the pressure curve applied for dyntopo detail size */
874   float dyntopo_pixel_radius;
875 
876   bool is_last_valid;
877 
878   bool pen_flip;
879   bool invert;
880   float pressure;
881   float bstrength;
882   float normal_weight; /* from brush (with optional override) */
883   float x_tilt;
884   float y_tilt;
885 
886   /* Position of the mouse corresponding to the stroke location, modified by the paint_stroke
887    * operator according to the stroke type. */
888   float mouse[2];
889   /* Position of the mouse event in screen space, not modified by the stroke type. */
890   float mouse_event[2];
891 
892   float (*prev_colors)[4];
893 
894   /* The rest is temporary storage that isn't saved as a property */
895 
896   bool first_time; /* Beginning of stroke may do some things special */
897 
898   /* from ED_view3d_ob_project_mat_get() */
899   float projection_mat[4][4];
900 
901   /* Clean this up! */
902   struct ViewContext *vc;
903   const struct Brush *brush;
904 
905   float special_rotation;
906   float grab_delta[3], grab_delta_symmetry[3];
907   float old_grab_location[3], orig_grab_location[3];
908 
909   /* screen-space rotation defined by mouse motion */
910   float rake_rotation[4], rake_rotation_symmetry[4];
911   bool is_rake_rotation_valid;
912   struct SculptRakeData rake_data;
913 
914   /* Face Sets */
915   int paint_face_set;
916 
917   /* Symmetry index between 0 and 7 bit combo 0 is Brush only;
918    * 1 is X mirror; 2 is Y mirror; 3 is XY; 4 is Z; 5 is XZ; 6 is YZ; 7 is XYZ */
919   int symmetry;
920   int mirror_symmetry_pass; /* the symmetry pass we are currently on between 0 and 7*/
921   float true_view_normal[3];
922   float view_normal[3];
923 
924   /* sculpt_normal gets calculated by calc_sculpt_normal(), then the
925    * sculpt_normal_symm gets updated quickly with the usual symmetry
926    * transforms */
927   float sculpt_normal[3];
928   float sculpt_normal_symm[3];
929 
930   /* Used for area texture mode, local_mat gets calculated by
931    * calc_brush_local_mat() and used in tex_strength(). */
932   float brush_local_mat[4][4];
933 
934   float plane_offset[3]; /* used to shift the plane around when doing tiled strokes */
935   int tile_pass;
936 
937   float last_center[3];
938   int radial_symmetry_pass;
939   float symm_rot_mat[4][4];
940   float symm_rot_mat_inv[4][4];
941   bool original;
942   float anchored_location[3];
943 
944   /* Paint Brush. */
945   struct {
946     float hardness;
947     float flow;
948     float wet_mix;
949     float wet_persistence;
950     float density;
951   } paint_brush;
952 
953   /* Pose brush */
954   struct SculptPoseIKChain *pose_ik_chain;
955 
956   /* Enhance Details. */
957   float (*detail_directions)[3];
958 
959   /* Clay Thumb brush */
960   /* Angle of the front tilting plane of the brush to simulate clay accumulation. */
961   float clay_thumb_front_angle;
962   /* Stores pressure samples to get an stabilized strength and radius variation. */
963   float clay_pressure_stabilizer[SCULPT_CLAY_STABILIZER_LEN];
964   int clay_pressure_stabilizer_index;
965 
966   /* Cloth brush */
967   struct SculptClothSimulation *cloth_sim;
968   float initial_location[3];
969   float true_initial_location[3];
970   float initial_normal[3];
971   float true_initial_normal[3];
972 
973   /* Boundary brush */
974   struct SculptBoundary *boundaries[PAINT_SYMM_AREAS];
975 
976   /* Surface Smooth Brush */
977   /* Stores the displacement produced by the laplacian step of HC smooth. */
978   float (*surface_smooth_laplacian_disp)[3];
979 
980   /* Layer brush */
981   float *layer_displacement_factor;
982 
983   float vertex_rotation; /* amount to rotate the vertices when using rotate brush */
984   struct Dial *dial;
985 
986   char saved_active_brush_name[MAX_ID_NAME];
987   char saved_mask_brush_tool;
988   int saved_smooth_size; /* smooth tool copies the size of the current tool */
989   bool alt_smooth;
990 
991   float plane_trim_squared;
992 
993   bool supports_gravity;
994   float true_gravity_direction[3];
995   float gravity_direction[3];
996 
997   /* Auto-masking. */
998   AutomaskingCache *automasking;
999 
1000   float stroke_local_mat[4][4];
1001   float multiplane_scrape_angle;
1002 
1003   float wet_mix_prev_color[4];
1004   float density_seed;
1005 
1006   rcti previous_r; /* previous redraw rectangle */
1007   rcti current_r;  /* current redraw rectangle */
1008 
1009 } StrokeCache;
1010 
1011 /* Sculpt Filters */
1012 typedef enum SculptFilterOrientation {
1013   SCULPT_FILTER_ORIENTATION_LOCAL = 0,
1014   SCULPT_FILTER_ORIENTATION_WORLD = 1,
1015   SCULPT_FILTER_ORIENTATION_VIEW = 2,
1016 } SculptFilterOrientation;
1017 
1018 void SCULPT_filter_to_orientation_space(float r_v[3], struct FilterCache *filter_cache);
1019 void SCULPT_filter_to_object_space(float r_v[3], struct FilterCache *filter_cache);
1020 void SCULPT_filter_zero_disabled_axis_components(float r_v[3], struct FilterCache *filter_cache);
1021 
1022 typedef struct FilterCache {
1023   bool enabled_axis[3];
1024   bool enabled_force_axis[3];
1025   int random_seed;
1026 
1027   /* Used for alternating between filter operations in filters that need to apply different ones to
1028    * achieve certain effects. */
1029   int iteration_count;
1030 
1031   /* Stores the displacement produced by the laplacian step of HC smooth. */
1032   float (*surface_smooth_laplacian_disp)[3];
1033   float surface_smooth_shape_preservation;
1034   float surface_smooth_current_vertex;
1035 
1036   /* Sharpen mesh filter. */
1037   float sharpen_smooth_ratio;
1038   float sharpen_intensify_detail_strength;
1039   int sharpen_curvature_smooth_iterations;
1040   float *sharpen_factor;
1041   float (*detail_directions)[3];
1042 
1043   /* Filter orientaiton. */
1044   SculptFilterOrientation orientation;
1045   float obmat[4][4];
1046   float obmat_inv[4][4];
1047   float viewmat[4][4];
1048   float viewmat_inv[4][4];
1049 
1050   /* Displacement eraser. */
1051   float (*limit_surface_co)[3];
1052 
1053   /* unmasked nodes */
1054   PBVHNode **nodes;
1055   int totnode;
1056 
1057   /* Cloth filter. */
1058   SculptClothSimulation *cloth_sim;
1059   float cloth_sim_pinch_point[3];
1060 
1061   /* mask expand iteration caches */
1062   int mask_update_current_it;
1063   int mask_update_last_it;
1064   int *mask_update_it;
1065   float *normal_factor;
1066   float *edge_factor;
1067   float *prev_mask;
1068   float mask_expand_initial_co[3];
1069 
1070   int new_face_set;
1071   int *prev_face_set;
1072 
1073   int active_face_set;
1074 
1075   /* Auto-masking. */
1076   AutomaskingCache *automasking;
1077 } FilterCache;
1078 
1079 void SCULPT_cache_calc_brushdata_symm(StrokeCache *cache,
1080                                       const char symm,
1081                                       const char axis,
1082                                       const float angle);
1083 void SCULPT_cache_free(StrokeCache *cache);
1084 
1085 SculptUndoNode *SCULPT_undo_push_node(Object *ob, PBVHNode *node, SculptUndoType type);
1086 SculptUndoNode *SCULPT_undo_get_node(PBVHNode *node);
1087 SculptUndoNode *SCULPT_undo_get_first_node(void);
1088 void SCULPT_undo_push_begin(const char *name);
1089 void SCULPT_undo_push_end(void);
1090 void SCULPT_undo_push_end_ex(const bool use_nested_undo);
1091 
1092 void SCULPT_vertcos_to_key(Object *ob, KeyBlock *kb, const float (*vertCos)[3]);
1093 
1094 void SCULPT_update_object_bounding_box(struct Object *ob);
1095 
1096 bool SCULPT_get_redraw_rect(struct ARegion *region,
1097                             struct RegionView3D *rv3d,
1098                             Object *ob,
1099                             rcti *rect);
1100 
1101 /* Operators. */
1102 
1103 /* Gestures. */
1104 void SCULPT_OT_face_set_lasso_gesture(struct wmOperatorType *ot);
1105 void SCULPT_OT_face_set_box_gesture(struct wmOperatorType *ot);
1106 
1107 void SCULPT_OT_trim_lasso_gesture(struct wmOperatorType *ot);
1108 void SCULPT_OT_trim_box_gesture(struct wmOperatorType *ot);
1109 
1110 void SCULPT_OT_project_line_gesture(struct wmOperatorType *ot);
1111 
1112 /* Face Sets. */
1113 void SCULPT_OT_face_sets_randomize_colors(struct wmOperatorType *ot);
1114 void SCULPT_OT_face_sets_change_visibility(struct wmOperatorType *ot);
1115 void SCULPT_OT_face_sets_init(struct wmOperatorType *ot);
1116 void SCULPT_OT_face_sets_create(struct wmOperatorType *ot);
1117 void SCULPT_OT_face_sets_edit(struct wmOperatorType *ot);
1118 
1119 /* Transform. */
1120 void SCULPT_OT_set_pivot_position(struct wmOperatorType *ot);
1121 
1122 /* Mesh Filter. */
1123 void SCULPT_OT_mesh_filter(struct wmOperatorType *ot);
1124 
1125 /* Cloth Filter. */
1126 void SCULPT_OT_cloth_filter(struct wmOperatorType *ot);
1127 
1128 /* Color Filter. */
1129 void SCULPT_OT_color_filter(struct wmOperatorType *ot);
1130 
1131 /* Mask filter and Dirty Mask. */
1132 void SCULPT_OT_mask_filter(struct wmOperatorType *ot);
1133 void SCULPT_OT_dirty_mask(struct wmOperatorType *ot);
1134 
1135 /* Mask and Face Sets Expand. */
1136 void SCULPT_OT_mask_expand(struct wmOperatorType *ot);
1137 
1138 /* Detail size. */
1139 void SCULPT_OT_detail_flood_fill(struct wmOperatorType *ot);
1140 void SCULPT_OT_sample_detail_size(struct wmOperatorType *ot);
1141 void SCULPT_OT_set_detail_size(struct wmOperatorType *ot);
1142 
1143 /* Dyntopo. */
1144 void SCULPT_OT_dynamic_topology_toggle(struct wmOperatorType *ot);
1145