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) 2001-2002 by NaN Holding BV.
17  * All rights reserved.
18  */
19 
20 #pragma once
21 
22 /** \file
23  * \ingroup bke
24  *
25  * Basic design of the DerivedMesh system:
26  *
27  * DerivedMesh is a common set of interfaces for mesh systems.
28  *
29  * There are three main mesh data structures in Blender:
30  * #Mesh, #CDDerivedMesh and #BMesh.
31  *
32  * These, and a few others, all implement DerivedMesh interfaces,
33  * which contains unified drawing interfaces, a few utility interfaces,
34  * and a bunch of read-only interfaces intended mostly for conversion from
35  * one format to another.
36  *
37  * All Mesh structures in blender make use of CustomData, which is used to store
38  * per-element attributes and interpolate them (e.g. uvs, vcols, vgroups, etc).
39  *
40  * Mesh is the "serialized" structure, used for storing object-mode mesh data
41  * and also for saving stuff to disk.  Its interfaces are also what DerivedMesh
42  * uses to communicate with.
43  *
44  * CDDM is a little mesh library, that uses Mesh data structures in the backend.
45  * It's mostly used for modifiers, and has the advantages of not taking much
46  * resources.
47  *
48  * BMesh is a full-on brep, used for editmode, some modifiers, etc.  It's much
49  * more capable (if memory-intensive) then CDDM.
50  *
51  * DerivedMesh is somewhat hackish.  Many places assumes that a DerivedMesh is
52  * a CDDM (most of the time by simply copying it and converting it to one).
53  * CDDM is the original structure for modifiers, but has since been superseded
54  * by BMesh, at least for the foreseeable future.
55  */
56 
57 /*
58  * Note: This structure is read-only, for all practical purposes.
59  *       At some point in the future, we may want to consider
60  *       creating a replacement structure that implements a proper
61  *       abstract mesh kernel interface.  Or, we can leave this
62  *       as it is and stick with using BMesh and CDDM.
63  */
64 
65 #include "DNA_customdata_types.h"
66 #include "DNA_defs.h"
67 #include "DNA_meshdata_types.h"
68 
69 #include "BLI_compiler_attrs.h"
70 
71 #include "BKE_bvhutils.h"
72 #include "BKE_customdata.h"
73 
74 #ifdef __cplusplus
75 extern "C" {
76 #endif
77 
78 struct BMEditMesh;
79 struct CCGElem;
80 struct CCGKey;
81 struct CustomData_MeshMasks;
82 struct Depsgraph;
83 struct MEdge;
84 struct MFace;
85 struct MVert;
86 struct Mesh;
87 struct ModifierData;
88 struct Object;
89 struct Scene;
90 
91 /*
92  * Note: all mface interfaces now officially operate on tessellated data.
93  *       Also, the mface origindex layer indexes mpolys, not mfaces.
94  */
95 
96 /* keep in sync with MFace/MPoly types */
97 typedef struct DMFlagMat {
98   short mat_nr;
99   char flag;
100 } DMFlagMat;
101 
102 typedef enum DerivedMeshType {
103   DM_TYPE_CDDM,
104   DM_TYPE_CCGDM,
105 } DerivedMeshType;
106 
107 typedef enum DMDirtyFlag {
108   /* dm has valid tessellated faces, but tessellated CDDATA need to be updated. */
109   DM_DIRTY_TESS_CDLAYERS = 1 << 0,
110 
111   /* check this with modifier dependsOnNormals callback to see if normals need recalculation */
112   DM_DIRTY_NORMALS = 1 << 1,
113 } DMDirtyFlag;
114 
115 typedef struct DerivedMesh DerivedMesh;
116 struct DerivedMesh {
117   /** Private DerivedMesh data, only for internal DerivedMesh use */
118   CustomData vertData, edgeData, faceData, loopData, polyData;
119   int numVertData, numEdgeData, numTessFaceData, numLoopData, numPolyData;
120   int needsFree;    /* checked on ->release, is set to 0 for cached results */
121   int deformedOnly; /* set by modifier stack if only deformed from original */
122   DerivedMeshType type;
123   DMDirtyFlag dirty;
124 
125   /**
126    * \warning Typical access is done via #getLoopTriArray, #getNumLoopTri.
127    */
128   struct {
129     /* WARNING! swapping between array (ready-to-be-used data) and array_wip
130      * (where data is actually computed) shall always be protected by same
131      * lock as one used for looptris computing. */
132     struct MLoopTri *array, *array_wip;
133     int num;
134     int num_alloc;
135   } looptris;
136 
137   /* use for converting to BMesh which doesn't store bevel weight and edge crease by default */
138   char cd_flag;
139 
140   short tangent_mask; /* which tangent layers are calculated */
141 
142   /** Calculate vert and face normals */
143   void (*calcNormals)(DerivedMesh *dm);
144 
145   /** Loop tessellation cache (WARNING! Only call inside threading-protected code!) */
146   void (*recalcLoopTri)(DerivedMesh *dm);
147   /** accessor functions */
148   const struct MLoopTri *(*getLoopTriArray)(DerivedMesh *dm);
149   int (*getNumLoopTri)(DerivedMesh *dm);
150 
151   /* Misc. Queries */
152 
153   /* Also called in Editmode */
154   int (*getNumVerts)(DerivedMesh *dm);
155   int (*getNumEdges)(DerivedMesh *dm);
156   int (*getNumTessFaces)(DerivedMesh *dm);
157   int (*getNumLoops)(DerivedMesh *dm);
158   int (*getNumPolys)(DerivedMesh *dm);
159 
160   /** Copy a single vert/edge/tessellated face from the derived mesh into
161    * ``*r_{vert/edge/face}``. note that the current implementation
162    * of this function can be quite slow, iterating over all
163    * elements (editmesh)
164    */
165   void (*getVert)(DerivedMesh *dm, int index, struct MVert *r_vert);
166   void (*getEdge)(DerivedMesh *dm, int index, struct MEdge *r_edge);
167   void (*getTessFace)(DerivedMesh *dm, int index, struct MFace *r_face);
168 
169   /** Return a pointer to the entire array of verts/edges/face from the
170    * derived mesh. if such an array does not exist yet, it will be created,
171    * and freed on the next ->release(). consider using getVert/Edge/Face if
172    * you are only interested in a few verts/edges/faces.
173    */
174   struct MVert *(*getVertArray)(DerivedMesh *dm);
175   struct MEdge *(*getEdgeArray)(DerivedMesh *dm);
176   struct MFace *(*getTessFaceArray)(DerivedMesh *dm);
177   struct MLoop *(*getLoopArray)(DerivedMesh *dm);
178   struct MPoly *(*getPolyArray)(DerivedMesh *dm);
179 
180   /** Copy all verts/edges/faces from the derived mesh into
181    * *{vert/edge/face}_r (must point to a buffer large enough)
182    */
183   void (*copyVertArray)(DerivedMesh *dm, struct MVert *r_vert);
184   void (*copyEdgeArray)(DerivedMesh *dm, struct MEdge *r_edge);
185   void (*copyTessFaceArray)(DerivedMesh *dm, struct MFace *r_face);
186   void (*copyLoopArray)(DerivedMesh *dm, struct MLoop *r_loop);
187   void (*copyPolyArray)(DerivedMesh *dm, struct MPoly *r_poly);
188 
189   /** Return a copy of all verts/edges/faces from the derived mesh
190    * it is the caller's responsibility to free the returned pointer
191    */
192   struct MVert *(*dupVertArray)(DerivedMesh *dm);
193   struct MEdge *(*dupEdgeArray)(DerivedMesh *dm);
194   struct MFace *(*dupTessFaceArray)(DerivedMesh *dm);
195   struct MLoop *(*dupLoopArray)(DerivedMesh *dm);
196   struct MPoly *(*dupPolyArray)(DerivedMesh *dm);
197 
198   /** Return a pointer to a single element of vert/edge/face custom data
199    * from the derived mesh (this gives a pointer to the actual data, not
200    * a copy)
201    */
202   void *(*getVertData)(DerivedMesh *dm, int index, int type);
203   void *(*getEdgeData)(DerivedMesh *dm, int index, int type);
204   void *(*getTessFaceData)(DerivedMesh *dm, int index, int type);
205   void *(*getPolyData)(DerivedMesh *dm, int index, int type);
206 
207   /** Return a pointer to the entire array of vert/edge/face custom data
208    * from the derived mesh (this gives a pointer to the actual data, not
209    * a copy)
210    */
211   void *(*getVertDataArray)(DerivedMesh *dm, int type);
212   void *(*getEdgeDataArray)(DerivedMesh *dm, int type);
213   void *(*getTessFaceDataArray)(DerivedMesh *dm, int type);
214   void *(*getLoopDataArray)(DerivedMesh *dm, int type);
215   void *(*getPolyDataArray)(DerivedMesh *dm, int type);
216 
217   /** Retrieves the base CustomData structures for
218    * verts/edges/tessfaces/loops/faces. */
219   CustomData *(*getVertDataLayout)(DerivedMesh *dm);
220   CustomData *(*getEdgeDataLayout)(DerivedMesh *dm);
221   CustomData *(*getTessFaceDataLayout)(DerivedMesh *dm);
222   CustomData *(*getLoopDataLayout)(DerivedMesh *dm);
223   CustomData *(*getPolyDataLayout)(DerivedMesh *dm);
224 
225   /** Optional grid access for subsurf */
226   int (*getNumGrids)(DerivedMesh *dm);
227   int (*getGridSize)(DerivedMesh *dm);
228   struct CCGElem **(*getGridData)(DerivedMesh *dm);
229   int *(*getGridOffset)(DerivedMesh *dm);
230   void (*getGridKey)(DerivedMesh *dm, struct CCGKey *key);
231   DMFlagMat *(*getGridFlagMats)(DerivedMesh *dm);
232   unsigned int **(*getGridHidden)(DerivedMesh *dm);
233 
234   /** Direct Access Operations
235    * - Can be undefined
236    * - Must be defined for modifiers that only deform however */
237 
238   /** Get vertex location, undefined if index is not valid */
239   void (*getVertCo)(DerivedMesh *dm, int index, float r_co[3]);
240 
241   /** Get smooth vertex normal, undefined if index is not valid */
242   void (*getVertNo)(DerivedMesh *dm, int index, float r_no[3]);
243   void (*getPolyNo)(DerivedMesh *dm, int index, float r_no[3]);
244 
245   /** Get a map of vertices to faces
246    */
247   const struct MeshElemMap *(*getPolyMap)(struct Object *ob, DerivedMesh *dm);
248 
249   /** Release reference to the DerivedMesh. This function decides internally
250    * if the DerivedMesh will be freed, or cached for later use. */
251   void (*release)(DerivedMesh *dm);
252 };
253 
254 void DM_init_funcs(DerivedMesh *dm);
255 
256 void DM_init(DerivedMesh *dm,
257              DerivedMeshType type,
258              int numVerts,
259              int numEdges,
260              int numTessFaces,
261              int numLoops,
262              int numPolys);
263 
264 void DM_from_template_ex(DerivedMesh *dm,
265                          DerivedMesh *source,
266                          DerivedMeshType type,
267                          int numVerts,
268                          int numEdges,
269                          int numTessFaces,
270                          int numLoops,
271                          int numPolys,
272                          const struct CustomData_MeshMasks *mask);
273 void DM_from_template(DerivedMesh *dm,
274                       DerivedMesh *source,
275                       DerivedMeshType type,
276                       int numVerts,
277                       int numEdges,
278                       int numTessFaces,
279                       int numLoops,
280                       int numPolys);
281 
282 /**
283  * Utility function to release a DerivedMesh's layers
284  * returns true if DerivedMesh has to be released by the backend, false otherwise.
285  */
286 bool DM_release(DerivedMesh *dm);
287 
288 void DM_set_only_copy(DerivedMesh *dm, const struct CustomData_MeshMasks *mask);
289 
290 /* adds a vertex/edge/face custom data layer to a DerivedMesh, optionally
291  * backed by an external data array
292  * alloctype defines how the layer is allocated or copied, and how it is
293  * freed, see BKE_customdata.h for the different options
294  */
295 void DM_add_vert_layer(struct DerivedMesh *dm, int type, eCDAllocType alloctype, void *layer);
296 void DM_add_edge_layer(struct DerivedMesh *dm, int type, eCDAllocType alloctype, void *layer);
297 void DM_add_tessface_layer(struct DerivedMesh *dm, int type, eCDAllocType alloctype, void *layer);
298 void DM_add_loop_layer(DerivedMesh *dm, int type, eCDAllocType alloctype, void *layer);
299 void DM_add_poly_layer(struct DerivedMesh *dm, int type, eCDAllocType alloctype, void *layer);
300 
301 /* custom data access functions
302  * return pointer to data from first layer which matches type
303  * if they return NULL for valid indices, data doesn't exist
304  * note these return pointers - any change modifies the internals of the mesh
305  */
306 void *DM_get_vert_data(struct DerivedMesh *dm, int index, int type);
307 void *DM_get_edge_data(struct DerivedMesh *dm, int index, int type);
308 void *DM_get_tessface_data(struct DerivedMesh *dm, int index, int type);
309 void *DM_get_poly_data(struct DerivedMesh *dm, int index, int type);
310 
311 /* custom data layer access functions
312  * return pointer to first data layer which matches type (a flat array)
313  * if they return NULL, data doesn't exist
314  * note these return pointers - any change modifies the internals of the mesh
315  */
316 void *DM_get_vert_data_layer(struct DerivedMesh *dm, int type);
317 void *DM_get_edge_data_layer(struct DerivedMesh *dm, int type);
318 void *DM_get_tessface_data_layer(struct DerivedMesh *dm, int type);
319 void *DM_get_poly_data_layer(struct DerivedMesh *dm, int type);
320 void *DM_get_loop_data_layer(struct DerivedMesh *dm, int type);
321 
322 /* custom data copy functions
323  * copy count elements from source_index in source to dest_index in dest
324  * these copy all layers for which the CD_FLAG_NOCOPY flag is not set
325  */
326 void DM_copy_vert_data(struct DerivedMesh *source,
327                        struct DerivedMesh *dest,
328                        int source_index,
329                        int dest_index,
330                        int count);
331 
332 /*sets up mpolys for a DM based on face iterators in source*/
333 void DM_DupPolys(DerivedMesh *source, DerivedMesh *target);
334 
335 void DM_ensure_normals(DerivedMesh *dm);
336 
337 void DM_ensure_looptri_data(DerivedMesh *dm);
338 
339 void DM_interp_vert_data(struct DerivedMesh *source,
340                          struct DerivedMesh *dest,
341                          int *src_indices,
342                          float *weights,
343                          int count,
344                          int dest_index);
345 
346 void mesh_get_mapped_verts_coords(struct Mesh *me_eval, float (*r_cos)[3], const int totcos);
347 
348 /* same as above but wont use render settings */
349 struct Mesh *editbmesh_get_eval_cage(struct Depsgraph *depsgraph,
350                                      struct Scene *scene,
351                                      struct Object *,
352                                      struct BMEditMesh *em,
353                                      const struct CustomData_MeshMasks *dataMask);
354 struct Mesh *editbmesh_get_eval_cage_from_orig(struct Depsgraph *depsgraph,
355                                                struct Scene *scene,
356                                                struct Object *obedit,
357                                                const struct CustomData_MeshMasks *dataMask);
358 struct Mesh *editbmesh_get_eval_cage_and_final(struct Depsgraph *depsgraph,
359                                                struct Scene *scene,
360                                                struct Object *,
361                                                struct BMEditMesh *em,
362                                                const struct CustomData_MeshMasks *dataMask,
363                                                struct Mesh **r_final);
364 
365 float (*editbmesh_vert_coords_alloc(struct BMEditMesh *em, int *r_vert_len))[3];
366 bool editbmesh_modifier_is_enabled(struct Scene *scene,
367                                    struct ModifierData *md,
368                                    bool has_prev_mesh);
369 void makeDerivedMesh(struct Depsgraph *depsgraph,
370                      struct Scene *scene,
371                      struct Object *ob,
372                      struct BMEditMesh *em,
373                      const struct CustomData_MeshMasks *dataMask);
374 
375 void DM_calc_loop_tangents(DerivedMesh *dm,
376                            bool calc_active_tangent,
377                            const char (*tangent_names)[MAX_NAME],
378                            int tangent_names_len);
379 
380 /* debug only */
381 #ifndef NDEBUG
382 char *DM_debug_info(DerivedMesh *dm);
383 void DM_debug_print(DerivedMesh *dm);
384 
385 bool DM_is_valid(DerivedMesh *dm);
386 #endif
387 
388 #ifdef __cplusplus
389 }
390 #endif
391