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 #pragma once
17 
18 /** \file
19  * \ingroup bke
20  */
21 
22 #include "BKE_customdata.h"
23 #include "BLI_compiler_attrs.h"
24 #include "DNA_modifier_types.h" /* needed for all enum typdefs */
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 struct ARegionType;
31 struct BMEditMesh;
32 struct BlendDataReader;
33 struct BlendWriter;
34 struct CustomData_MeshMasks;
35 struct DepsNodeHandle;
36 struct Depsgraph;
37 struct ID;
38 struct ListBase;
39 struct Main;
40 struct Mesh;
41 struct ModifierData;
42 struct Object;
43 struct Scene;
44 struct bArmature;
45 
46 typedef enum {
47   /* Should not be used, only for None modifier type */
48   eModifierTypeType_None,
49 
50   /**
51    * Modifier only does deformation, implies that modifier
52    * type should have a valid deformVerts function. OnlyDeform
53    * style modifiers implicitly accept either mesh or CV
54    * input but should still declare flags appropriately.
55    */
56   eModifierTypeType_OnlyDeform,
57 
58   /** Modifier adds geometry. */
59   eModifierTypeType_Constructive,
60   /* Modifier can add and remove geometry. */
61   eModifierTypeType_Nonconstructive,
62 
63   /**
64    * Both deformVerts & applyModifier are valid calls
65    * used for particles modifier that doesn't actually modify the object
66    * unless it's a mesh and can be exploded -> curve can also emit particles
67    */
68   eModifierTypeType_DeformOrConstruct,
69 
70   /**
71    * Like eModifierTypeType_Nonconstructive, but does not affect the geometry
72    * of the object, rather some of its CustomData layers.
73    * E.g. UVProject and WeightVG modifiers. */
74   eModifierTypeType_NonGeometrical,
75 } ModifierTypeType;
76 
77 typedef enum {
78   eModifierTypeFlag_AcceptsMesh = (1 << 0),
79   eModifierTypeFlag_AcceptsCVs = (1 << 1),
80   eModifierTypeFlag_SupportsMapping = (1 << 2),
81   eModifierTypeFlag_SupportsEditmode = (1 << 3),
82 
83   /**
84    * For modifiers that support editmode this determines if the
85    * modifier should be enabled by default in editmode. This should
86    * only be used by modifiers that are relatively speedy and
87    * also generally used in editmode, otherwise let the user enable
88    * it by hand.
89    */
90   eModifierTypeFlag_EnableInEditmode = (1 << 4),
91 
92   /**
93    * For modifiers that require original data and so cannot
94    * be placed after any non-deformative modifier.
95    */
96   eModifierTypeFlag_RequiresOriginalData = (1 << 5),
97 
98   /**
99    * For modifiers that support pointcache,
100    * so we can check to see if it has files we need to deal with.
101    */
102   eModifierTypeFlag_UsesPointCache = (1 << 6),
103 
104   /** For physics modifiers, max one per type */
105   eModifierTypeFlag_Single = (1 << 7),
106 
107   /** Some modifier can't be added manually by user */
108   eModifierTypeFlag_NoUserAdd = (1 << 8),
109 
110   /** For modifiers that use CD_PREVIEW_MCOL for preview. */
111   eModifierTypeFlag_UsesPreview = (1 << 9),
112   eModifierTypeFlag_AcceptsVertexCosOnly = (1 << 10),
113 
114   /** Accepts #BMesh input (without conversion). */
115   eModifierTypeFlag_AcceptsBMesh = (1 << 11),
116 } ModifierTypeFlag;
117 
118 typedef void (*IDWalkFunc)(void *userData, struct Object *ob, struct ID **idpoin, int cb_flag);
119 typedef void (*TexWalkFunc)(void *userData,
120                             struct Object *ob,
121                             struct ModifierData *md,
122                             const char *propname);
123 
124 typedef enum ModifierApplyFlag {
125   /** Render time. */
126   MOD_APPLY_RENDER = 1 << 0,
127   /** Result of evaluation will be cached, so modifier might
128    * want to cache data for quick updates (used by subsurf) */
129   MOD_APPLY_USECACHE = 1 << 1,
130   /** Modifier evaluated for undeformed texture coordinates */
131   MOD_APPLY_ORCO = 1 << 2,
132   /** Ignore scene simplification flag and use subdivisions
133    * level set in multires modifier. */
134   MOD_APPLY_IGNORE_SIMPLIFY = 1 << 3,
135   /** The effect of this modifier will be applied to the base mesh
136    * The modifier itself will be removed from the modifier stack.
137    * This flag can be checked to ignore rendering display data to the mesh.
138    * See `OBJECT_OT_modifier_apply` operator. */
139   MOD_APPLY_TO_BASE_MESH = 1 << 4,
140 } ModifierApplyFlag;
141 
142 typedef struct ModifierUpdateDepsgraphContext {
143   struct Scene *scene;
144   struct Object *object;
145   struct DepsNodeHandle *node;
146 } ModifierUpdateDepsgraphContext;
147 
148 /* Contains the information for deformXXX and applyXXX functions below that
149  * doesn't change between consecutive modifiers. */
150 typedef struct ModifierEvalContext {
151   struct Depsgraph *depsgraph;
152   struct Object *object;
153   ModifierApplyFlag flag;
154 } ModifierEvalContext;
155 
156 typedef struct ModifierTypeInfo {
157   /* The user visible name for this modifier */
158   char name[32];
159 
160   /* The DNA struct name for the modifier data type, used to
161    * write the DNA data out.
162    */
163   char structName[32];
164 
165   /* The size of the modifier data type, used by allocation. */
166   int structSize;
167 
168   /* StructRNA of this modifier. This is typically something like RNA_*Modifier. */
169   struct StructRNA *srna;
170 
171   ModifierTypeType type;
172   ModifierTypeFlag flags;
173 
174   /* Icon of the modifier. Usually something like ICON_MOD_*. */
175   int icon;
176 
177   /********************* Non-optional functions *********************/
178 
179   /**
180    * Copy instance data for this modifier type. Should copy all user
181    * level settings to the target modifier.
182    *
183    * \param flag: Copying options (see BKE_lib_id.h's LIB_ID_COPY_... flags for more).
184    */
185   void (*copyData)(const struct ModifierData *md, struct ModifierData *target, const int flag);
186 
187   /********************* Deform modifier functions *********************/
188 
189   /**
190    * Only for deform types, should apply the deformation
191    * to the given vertex array. If the deformer requires information from
192    * the object it can obtain it from the mesh argument if non-NULL,
193    * and otherwise the ob argument.
194    */
195   void (*deformVerts)(struct ModifierData *md,
196                       const struct ModifierEvalContext *ctx,
197                       struct Mesh *mesh,
198                       float (*vertexCos)[3],
199                       int numVerts);
200 
201   /**
202    * Like deformMatricesEM but called from object mode (for supporting modifiers in sculpt mode).
203    */
204   void (*deformMatrices)(struct ModifierData *md,
205                          const struct ModifierEvalContext *ctx,
206                          struct Mesh *mesh,
207                          float (*vertexCos)[3],
208                          float (*defMats)[3][3],
209                          int numVerts);
210   /**
211    * Like deformVerts but called during editmode (for supporting modifiers)
212    */
213   void (*deformVertsEM)(struct ModifierData *md,
214                         const struct ModifierEvalContext *ctx,
215                         struct BMEditMesh *editData,
216                         struct Mesh *mesh,
217                         float (*vertexCos)[3],
218                         int numVerts);
219 
220   /* Set deform matrix per vertex for crazy-space correction */
221   void (*deformMatricesEM)(struct ModifierData *md,
222                            const struct ModifierEvalContext *ctx,
223                            struct BMEditMesh *editData,
224                            struct Mesh *mesh,
225                            float (*vertexCos)[3],
226                            float (*defMats)[3][3],
227                            int numVerts);
228 
229   /********************* Non-deform modifier functions *********************/
230 
231   /**
232    * For non-deform types: apply the modifier and return a mesh data-block.
233    *
234    * The mesh argument should always be non-NULL; the modifier should use the
235    * passed in mesh data-block rather than object->data, as it contains the mesh
236    * with modifier applied up to this point.
237    *
238    * The modifier may modify and return the mesh argument, but must not free it
239    * and must ensure any referenced data layers are converted to non-referenced
240    * before modification.
241    */
242   struct Mesh *(*modifyMesh)(struct ModifierData *md,
243                              const struct ModifierEvalContext *ctx,
244                              struct Mesh *mesh);
245   struct Hair *(*modifyHair)(struct ModifierData *md,
246                              const struct ModifierEvalContext *ctx,
247                              struct Hair *hair);
248   struct PointCloud *(*modifyPointCloud)(struct ModifierData *md,
249                                          const struct ModifierEvalContext *ctx,
250                                          struct PointCloud *pointcloud);
251   struct Volume *(*modifyVolume)(struct ModifierData *md,
252                                  const struct ModifierEvalContext *ctx,
253                                  struct Volume *volume);
254 
255   /********************* Optional functions *********************/
256 
257   /**
258    * Initialize new instance data for this modifier type, this function
259    * should set modifier variables to their default values.
260    *
261    * This function is optional.
262    */
263   void (*initData)(struct ModifierData *md);
264 
265   /**
266    * Should add to passed \a r_cddata_masks the data types that this
267    * modifier needs. If (mask & (1 << (layer type))) != 0, this modifier
268    * needs that custom data layer. It can change required layers
269    * depending on the modifier's settings.
270    *
271    * Note that this means extra data (e.g. vertex groups) - it is assumed
272    * that all modifiers need mesh data and deform modifiers need vertex
273    * coordinates.
274    *
275    * If this function is not present, it is assumed that no extra data is needed.
276    *
277    * This function is optional.
278    */
279   void (*requiredDataMask)(struct Object *ob,
280                            struct ModifierData *md,
281                            struct CustomData_MeshMasks *r_cddata_masks);
282 
283   /**
284    * Free internal modifier data variables, this function should
285    * not free the md variable itself.
286    *
287    * This function is responsible for freeing the runtime data as well.
288    *
289    * This function is optional.
290    */
291   void (*freeData)(struct ModifierData *md);
292 
293   /**
294    * Return a boolean value indicating if this modifier is able to be
295    * calculated based on the modifier data. This is *not* regarding the
296    * md->flag, that is tested by the system, this is just if the data
297    * validates (for example, a lattice will return false if the lattice
298    * object is not defined).
299    *
300    * This function is optional (assumes never disabled if not present).
301    */
302   bool (*isDisabled)(const struct Scene *scene, struct ModifierData *md, bool userRenderParams);
303 
304   /**
305    * Add the appropriate relations to the dependency graph.
306    *
307    * This function is optional.
308    */
309   void (*updateDepsgraph)(struct ModifierData *md, const ModifierUpdateDepsgraphContext *ctx);
310 
311   /**
312    * Should return true if the modifier needs to be recalculated on time
313    * changes.
314    *
315    * This function is optional (assumes false if not present).
316    */
317   bool (*dependsOnTime)(struct ModifierData *md);
318 
319   /**
320    * True when a deform modifier uses normals, the requiredDataMask
321    * cant be used here because that refers to a normal layer whereas
322    * in this case we need to know if the deform modifier uses normals.
323    *
324    * this is needed because applying 2 deform modifiers will give the
325    * second modifier bogus normals.
326    */
327   bool (*dependsOnNormals)(struct ModifierData *md);
328 
329   /**
330    * Should call the given walk function with a pointer to each ID
331    * pointer (i.e. each data-block pointer) that the modifier data
332    * stores. This is used for linking on file load and for
333    * unlinking data-blocks or forwarding data-block references.
334    *
335    * This function is optional.
336    */
337   void (*foreachIDLink)(struct ModifierData *md,
338                         struct Object *ob,
339                         IDWalkFunc walk,
340                         void *userData);
341 
342   /**
343    * Should call the given walk function for each texture that the
344    * modifier data stores. This is used for finding all textures in
345    * the context for the UI.
346    *
347    * This function is optional. If it is not present, it will be
348    * assumed the modifier has no textures.
349    */
350   void (*foreachTexLink)(struct ModifierData *md,
351                          struct Object *ob,
352                          TexWalkFunc walk,
353                          void *userData);
354 
355   /**
356    * Free given run-time data.
357    *
358    * This data is coming from a modifier of the corresponding type, but actual
359    * modifier data is not known here.
360    *
361    * Notes:
362    *  - The data itself is to be de-allocated as well.
363    *  - This callback is allowed to receive NULL pointer as a data, so it's
364    *    more like "ensure the data is freed".
365    */
366   void (*freeRuntimeData)(void *runtime_data);
367 
368   /** Register the panel types for the modifier's UI. */
369   void (*panelRegister)(struct ARegionType *region_type);
370 
371   /**
372    * Is called when the modifier is written to a file. The modifier data struct itself is written
373    * already.
374    *
375    * This method should write any additional arrays and referenced structs that should be
376    * stored in the file.
377    */
378   void (*blendWrite)(struct BlendWriter *writer, const struct ModifierData *md);
379 
380   /**
381    * Is called when the modifier is read from a file.
382    *
383    * It can be used to update pointers to arrays and other structs. Furthermore, fields that have
384    * not been written (e.g. runtime data) can be reset.
385    */
386   void (*blendRead)(struct BlendDataReader *reader, struct ModifierData *md);
387 } ModifierTypeInfo;
388 
389 /* Used to find a modifier's panel type. */
390 #define MODIFIER_TYPE_PANEL_PREFIX "MOD_PT_"
391 
392 /* Initialize modifier's global data (type info and some common global storages). */
393 void BKE_modifier_init(void);
394 
395 const ModifierTypeInfo *BKE_modifier_get_info(ModifierType type);
396 
397 /* For modifier UI panels. */
398 void BKE_modifier_type_panel_id(ModifierType type, char *r_idname);
399 
400 /* Modifier utility calls, do call through type pointer and return
401  * default values if pointer is optional.
402  */
403 struct ModifierData *BKE_modifier_new(int type);
404 
405 void BKE_modifier_free_ex(struct ModifierData *md, const int flag);
406 void BKE_modifier_free(struct ModifierData *md);
407 
408 /* Generate new UUID for the given modifier. */
409 void BKE_modifier_session_uuid_generate(struct ModifierData *md);
410 
411 bool BKE_modifier_unique_name(struct ListBase *modifiers, struct ModifierData *md);
412 
413 void BKE_modifier_copydata_generic(const struct ModifierData *md,
414                                    struct ModifierData *md_dst,
415                                    const int flag);
416 void BKE_modifier_copydata(struct ModifierData *md, struct ModifierData *target);
417 void BKE_modifier_copydata_ex(struct ModifierData *md,
418                               struct ModifierData *target,
419                               const int flag);
420 bool BKE_modifier_depends_ontime(struct ModifierData *md);
421 bool BKE_modifier_supports_mapping(struct ModifierData *md);
422 bool BKE_modifier_supports_cage(struct Scene *scene, struct ModifierData *md);
423 bool BKE_modifier_couldbe_cage(struct Scene *scene, struct ModifierData *md);
424 bool BKE_modifier_is_correctable_deformed(struct ModifierData *md);
425 bool BKE_modifier_is_same_topology(ModifierData *md);
426 bool BKE_modifier_is_non_geometrical(ModifierData *md);
427 bool BKE_modifier_is_enabled(const struct Scene *scene,
428                              struct ModifierData *md,
429                              int required_mode);
430 void BKE_modifier_set_error(struct ModifierData *md, const char *format, ...)
431     ATTR_PRINTF_FORMAT(2, 3);
432 bool BKE_modifier_is_preview(struct ModifierData *md);
433 
434 void BKE_modifiers_foreach_ID_link(struct Object *ob, IDWalkFunc walk, void *userData);
435 void BKE_modifiers_foreach_tex_link(struct Object *ob, TexWalkFunc walk, void *userData);
436 
437 struct ModifierData *BKE_modifiers_findby_type(struct Object *ob, ModifierType type);
438 struct ModifierData *BKE_modifiers_findby_name(struct Object *ob, const char *name);
439 void BKE_modifiers_clear_errors(struct Object *ob);
440 int BKE_modifiers_get_cage_index(struct Scene *scene,
441                                  struct Object *ob,
442                                  int *r_lastPossibleCageIndex,
443                                  bool is_virtual);
444 
445 bool BKE_modifiers_is_modifier_enabled(struct Object *ob, int modifierType);
446 bool BKE_modifiers_is_softbody_enabled(struct Object *ob);
447 bool BKE_modifiers_is_cloth_enabled(struct Object *ob);
448 bool BKE_modifiers_is_particle_enabled(struct Object *ob);
449 
450 struct Object *BKE_modifiers_is_deformed_by_armature(struct Object *ob);
451 struct Object *BKE_modifiers_is_deformed_by_meshdeform(struct Object *ob);
452 struct Object *BKE_modifiers_is_deformed_by_lattice(struct Object *ob);
453 struct Object *BKE_modifiers_is_deformed_by_curve(struct Object *ob);
454 bool BKE_modifiers_uses_multires(struct Object *ob);
455 bool BKE_modifiers_uses_armature(struct Object *ob, struct bArmature *arm);
456 bool BKE_modifiers_uses_subsurf_facedots(struct Scene *scene, struct Object *ob);
457 bool BKE_modifiers_is_correctable_deformed(struct Scene *scene, struct Object *ob);
458 void BKE_modifier_free_temporary_data(struct ModifierData *md);
459 
460 typedef struct CDMaskLink {
461   struct CDMaskLink *next;
462   struct CustomData_MeshMasks mask;
463 } CDMaskLink;
464 
465 /**
466  * Calculates and returns a linked list of CustomData_MeshMasks and modified
467  * final datamask, indicating the data required by each modifier in the stack
468  * pointed to by md for correct evaluation, assuming the data indicated by
469  * final_datamask is required at the end of the stack.
470  */
471 struct CDMaskLink *BKE_modifier_calc_data_masks(struct Scene *scene,
472                                                 struct Object *ob,
473                                                 struct ModifierData *md,
474                                                 struct CustomData_MeshMasks *final_datamask,
475                                                 int required_mode,
476                                                 ModifierData *previewmd,
477                                                 const struct CustomData_MeshMasks *previewmask);
478 struct ModifierData *BKE_modifier_get_last_preview(struct Scene *scene,
479                                                    struct ModifierData *md,
480                                                    int required_mode);
481 
482 typedef struct VirtualModifierData {
483   ArmatureModifierData amd;
484   CurveModifierData cmd;
485   LatticeModifierData lmd;
486   ShapeKeyModifierData smd;
487 } VirtualModifierData;
488 
489 struct ModifierData *BKE_modifiers_get_virtual_modifierlist(const struct Object *ob,
490                                                             struct VirtualModifierData *data);
491 
492 /** Ensure modifier correctness when changing ob->data. */
493 void BKE_modifiers_test_object(struct Object *ob);
494 
495 /* here for do_versions */
496 void BKE_modifier_mdef_compact_influences(struct ModifierData *md);
497 
498 void BKE_modifier_path_init(char *path, int path_maxlen, const char *name);
499 const char *BKE_modifier_path_relbase(struct Main *bmain, struct Object *ob);
500 const char *BKE_modifier_path_relbase_from_global(struct Object *ob);
501 
502 /* Accessors of original/evaluated modifiers. */
503 
504 /* For a given modifier data, get corresponding original one.
505  * If the modifier data is already original, return it as-is. */
506 struct ModifierData *BKE_modifier_get_original(struct ModifierData *md);
507 struct ModifierData *BKE_modifier_get_evaluated(struct Depsgraph *depsgraph,
508                                                 struct Object *object,
509                                                 struct ModifierData *md);
510 
511 /* wrappers for modifier callbacks that ensure valid normals */
512 
513 struct Mesh *BKE_modifier_modify_mesh(ModifierData *md,
514                                       const struct ModifierEvalContext *ctx,
515                                       struct Mesh *me);
516 
517 void BKE_modifier_deform_verts(ModifierData *md,
518                                const struct ModifierEvalContext *ctx,
519                                struct Mesh *me,
520                                float (*vertexCos)[3],
521                                int numVerts);
522 
523 void BKE_modifier_deform_vertsEM(ModifierData *md,
524                                  const struct ModifierEvalContext *ctx,
525                                  struct BMEditMesh *em,
526                                  struct Mesh *me,
527                                  float (*vertexCos)[3],
528                                  int numVerts);
529 
530 struct Mesh *BKE_modifier_get_evaluated_mesh_from_evaluated_object(struct Object *ob_eval,
531                                                                    const bool get_cage_mesh);
532 
533 void BKE_modifier_check_uuids_unique_and_report(const struct Object *object);
534 
535 #ifdef __cplusplus
536 }
537 #endif
538