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 #pragma once
20 
21 /** \file
22  * \ingroup bke
23  */
24 
25 #include "BKE_mesh_types.h"
26 #include "BLI_utildefines.h"
27 
28 struct BLI_Stack;
29 struct BMEditMesh;
30 struct BMesh;
31 struct BMeshCreateParams;
32 struct BMeshFromMeshParams;
33 struct BMeshToMeshParams;
34 struct BoundBox;
35 struct CustomData;
36 struct CustomData_MeshMasks;
37 struct Depsgraph;
38 struct EdgeHash;
39 struct ID;
40 struct KeyBlock;
41 struct LinkNode;
42 struct ListBase;
43 struct MDeformVert;
44 struct MDisps;
45 struct MEdge;
46 struct MFace;
47 struct MLoop;
48 struct MLoopTri;
49 struct MLoopUV;
50 struct MPoly;
51 struct MVert;
52 struct Main;
53 struct MemArena;
54 struct Mesh;
55 struct ModifierData;
56 struct Object;
57 struct PointCloud;
58 struct Scene;
59 
60 #ifdef __cplusplus
61 extern "C" {
62 #endif
63 
64 /* setting zero so we can catch bugs in OpenMP/BMesh */
65 #ifdef DEBUG
66 #  define BKE_MESH_OMP_LIMIT 0
67 #else
68 #  define BKE_MESH_OMP_LIMIT 10000
69 #endif
70 
71 /* *** mesh.c *** */
72 
73 struct BMesh *BKE_mesh_to_bmesh_ex(const struct Mesh *me,
74                                    const struct BMeshCreateParams *create_params,
75                                    const struct BMeshFromMeshParams *convert_params);
76 struct BMesh *BKE_mesh_to_bmesh(struct Mesh *me,
77                                 struct Object *ob,
78                                 const bool add_key_index,
79                                 const struct BMeshCreateParams *params);
80 
81 struct Mesh *BKE_mesh_from_bmesh_nomain(struct BMesh *bm,
82                                         const struct BMeshToMeshParams *params,
83                                         const struct Mesh *me_settings);
84 struct Mesh *BKE_mesh_from_bmesh_for_eval_nomain(struct BMesh *bm,
85                                                  const struct CustomData_MeshMasks *cd_mask_extra,
86                                                  const struct Mesh *me_settings);
87 
88 int poly_find_loop_from_vert(const struct MPoly *poly, const struct MLoop *loopstart, uint vert);
89 int poly_get_adj_loops_from_vert(const struct MPoly *poly,
90                                  const struct MLoop *mloop,
91                                  unsigned int vert,
92                                  unsigned int r_adj[2]);
93 
94 int BKE_mesh_edge_other_vert(const struct MEdge *e, int v);
95 void BKE_mesh_looptri_get_real_edges(const struct Mesh *mesh,
96                                      const struct MLoopTri *looptri,
97                                      int r_edges[3]);
98 
99 void BKE_mesh_free(struct Mesh *me);
100 void BKE_mesh_clear_geometry(struct Mesh *me);
101 struct Mesh *BKE_mesh_add(struct Main *bmain, const char *name);
102 void BKE_mesh_copy_settings(struct Mesh *me_dst, const struct Mesh *me_src);
103 void BKE_mesh_update_customdata_pointers(struct Mesh *me, const bool do_ensure_tess_cd);
104 void BKE_mesh_ensure_skin_customdata(struct Mesh *me);
105 
106 struct Mesh *BKE_mesh_new_nomain(
107     int verts_len, int edges_len, int tessface_len, int loops_len, int polys_len);
108 struct Mesh *BKE_mesh_new_nomain_from_template(const struct Mesh *me_src,
109                                                int verts_len,
110                                                int edges_len,
111                                                int tessface_len,
112                                                int loops_len,
113                                                int polys_len);
114 struct Mesh *BKE_mesh_new_nomain_from_template_ex(const struct Mesh *me_src,
115                                                   int verts_len,
116                                                   int edges_len,
117                                                   int tessface_len,
118                                                   int loops_len,
119                                                   int polys_len,
120                                                   struct CustomData_MeshMasks mask);
121 
122 void BKE_mesh_eval_delete(struct Mesh *mesh_eval);
123 
124 /* Performs copy for use during evaluation,
125  * optional referencing original arrays to reduce memory. */
126 struct Mesh *BKE_mesh_copy_for_eval(struct Mesh *source, bool reference);
127 
128 /* These functions construct a new Mesh,
129  * contrary to BKE_mesh_from_nurbs which modifies ob itself. */
130 struct Mesh *BKE_mesh_new_nomain_from_curve(struct Object *ob);
131 struct Mesh *BKE_mesh_new_nomain_from_curve_displist(struct Object *ob, struct ListBase *dispbase);
132 
133 bool BKE_mesh_ensure_facemap_customdata(struct Mesh *me);
134 bool BKE_mesh_clear_facemap_customdata(struct Mesh *me);
135 
136 float (*BKE_mesh_orco_verts_get(struct Object *ob))[3];
137 void BKE_mesh_orco_verts_transform(struct Mesh *me, float (*orco)[3], int totvert, int invert);
138 int test_index_face(struct MFace *mface, struct CustomData *mfdata, int mfindex, int nr);
139 struct Mesh *BKE_mesh_from_object(struct Object *ob);
140 void BKE_mesh_assign_object(struct Main *bmain, struct Object *ob, struct Mesh *me);
141 void BKE_mesh_from_metaball(struct ListBase *lb, struct Mesh *me);
142 int BKE_mesh_nurbs_to_mdata(struct Object *ob,
143                             struct MVert **r_allvert,
144                             int *r_totvert,
145                             struct MEdge **r_alledge,
146                             int *r_totedge,
147                             struct MLoop **r_allloop,
148                             struct MPoly **r_allpoly,
149                             int *r_totloop,
150                             int *r_totpoly);
151 int BKE_mesh_nurbs_displist_to_mdata(struct Object *ob,
152                                      const struct ListBase *dispbase,
153                                      struct MVert **r_allvert,
154                                      int *r_totvert,
155                                      struct MEdge **r_alledge,
156                                      int *r_totedge,
157                                      struct MLoop **r_allloop,
158                                      struct MPoly **r_allpoly,
159                                      struct MLoopUV **r_alluv,
160                                      int *r_totloop,
161                                      int *r_totpoly);
162 void BKE_mesh_from_nurbs_displist(struct Main *bmain,
163                                   struct Object *ob,
164                                   struct ListBase *dispbase,
165                                   const char *obdata_name,
166                                   bool temporary);
167 void BKE_mesh_from_nurbs(struct Main *bmain, struct Object *ob);
168 void BKE_mesh_to_curve_nurblist(const struct Mesh *me,
169                                 struct ListBase *nurblist,
170                                 const int edge_users_test);
171 void BKE_mesh_to_curve(struct Main *bmain,
172                        struct Depsgraph *depsgraph,
173                        struct Scene *scene,
174                        struct Object *ob);
175 void BKE_pointcloud_from_mesh(struct Mesh *me, struct PointCloud *pointcloud);
176 void BKE_mesh_to_pointcloud(struct Main *bmain,
177                             struct Depsgraph *depsgraph,
178                             struct Scene *scene,
179                             struct Object *ob);
180 void BKE_mesh_from_pointcloud(struct PointCloud *pointcloud, struct Mesh *me);
181 void BKE_pointcloud_to_mesh(struct Main *bmain,
182                             struct Depsgraph *depsgraph,
183                             struct Scene *scene,
184                             struct Object *ob);
185 void BKE_mesh_material_index_remove(struct Mesh *me, short index);
186 bool BKE_mesh_material_index_used(struct Mesh *me, short index);
187 void BKE_mesh_material_index_clear(struct Mesh *me);
188 void BKE_mesh_material_remap(struct Mesh *me, const unsigned int *remap, unsigned int remap_len);
189 void BKE_mesh_smooth_flag_set(struct Mesh *me, const bool use_smooth);
190 
191 /* Needed after converting a mesh with subsurf optimal display to mesh. */
192 void BKE_mesh_edges_set_draw_render(struct Mesh *me);
193 
194 const char *BKE_mesh_cmp(struct Mesh *me1, struct Mesh *me2, float thresh);
195 
196 struct BoundBox *BKE_mesh_boundbox_get(struct Object *ob);
197 
198 void BKE_mesh_texspace_calc(struct Mesh *me);
199 void BKE_mesh_texspace_ensure(struct Mesh *me);
200 void BKE_mesh_texspace_get(struct Mesh *me, float r_loc[3], float r_size[3]);
201 void BKE_mesh_texspace_get_reference(struct Mesh *me,
202                                      short **r_texflag,
203                                      float **r_loc,
204                                      float **r_size);
205 void BKE_mesh_texspace_copy_from_object(struct Mesh *me, struct Object *ob);
206 
207 void BKE_mesh_split_faces(struct Mesh *mesh, bool free_loop_normals);
208 
209 /* Create new mesh from the given object at its current state.
210  * The owner of this mesh is unknown, it is up to the caller to decide.
211  *
212  * If preserve_all_data_layers is truth then the modifier stack is re-evaluated to ensure it
213  * preserves all possible custom data layers.
214  *
215  * NOTE: Dependency graph argument is required when preserve_all_data_layers is truth, and is
216  * ignored otherwise. */
217 struct Mesh *BKE_mesh_new_from_object(struct Depsgraph *depsgraph,
218                                       struct Object *object,
219                                       bool preserve_all_data_layers);
220 
221 /* This is a version of BKE_mesh_new_from_object() which stores mesh in the given main database.
222  * However, that function enforces object type to be a geometry one, and ensures a mesh is always
223  * generated, be it empty. */
224 struct Mesh *BKE_mesh_new_from_object_to_bmain(struct Main *bmain,
225                                                struct Depsgraph *depsgraph,
226                                                struct Object *object,
227                                                bool preserve_all_data_layers);
228 
229 struct Mesh *BKE_mesh_create_derived_for_modifier(struct Depsgraph *depsgraph,
230                                                   struct Scene *scene,
231                                                   struct Object *ob_eval,
232                                                   struct ModifierData *md_eval,
233                                                   int build_shapekey_layers);
234 
235 /* Copies a nomain-Mesh into an existing Mesh. */
236 void BKE_mesh_nomain_to_mesh(struct Mesh *mesh_src,
237                              struct Mesh *mesh_dst,
238                              struct Object *ob,
239                              const struct CustomData_MeshMasks *mask,
240                              bool take_ownership);
241 void BKE_mesh_nomain_to_meshkey(struct Mesh *mesh_src, struct Mesh *mesh_dst, struct KeyBlock *kb);
242 
243 /* vertex level transformations & checks (no derived mesh) */
244 
245 bool BKE_mesh_minmax(const struct Mesh *me, float r_min[3], float r_max[3]);
246 void BKE_mesh_transform(struct Mesh *me, const float mat[4][4], bool do_keys);
247 void BKE_mesh_translate(struct Mesh *me, const float offset[3], const bool do_keys);
248 
249 void BKE_mesh_tessface_calc(struct Mesh *mesh);
250 void BKE_mesh_tessface_ensure(struct Mesh *mesh);
251 void BKE_mesh_tessface_clear(struct Mesh *mesh);
252 
253 void BKE_mesh_do_versions_cd_flag_init(struct Mesh *mesh);
254 
255 void BKE_mesh_mselect_clear(struct Mesh *me);
256 void BKE_mesh_mselect_validate(struct Mesh *me);
257 int BKE_mesh_mselect_find(struct Mesh *me, int index, int type);
258 int BKE_mesh_mselect_active_get(struct Mesh *me, int type);
259 void BKE_mesh_mselect_active_set(struct Mesh *me, int index, int type);
260 
261 void BKE_mesh_count_selected_items(const struct Mesh *mesh, int r_count[3]);
262 
263 float (*BKE_mesh_vert_coords_alloc(const struct Mesh *mesh, int *r_vert_len))[3];
264 void BKE_mesh_vert_coords_get(const struct Mesh *mesh, float (*vert_coords)[3]);
265 
266 void BKE_mesh_vert_coords_apply_with_mat4(struct Mesh *mesh,
267                                           const float (*vert_coords)[3],
268                                           const float mat[4][4]);
269 void BKE_mesh_vert_coords_apply(struct Mesh *mesh, const float (*vert_coords)[3]);
270 void BKE_mesh_vert_normals_apply(struct Mesh *mesh, const short (*vert_normals)[3]);
271 
272 /* *** mesh_evaluate.c *** */
273 
274 void BKE_mesh_calc_normals_mapping_simple(struct Mesh *me);
275 void BKE_mesh_calc_normals_mapping(struct MVert *mverts,
276                                    int numVerts,
277                                    const struct MLoop *mloop,
278                                    const struct MPoly *mpolys,
279                                    int numLoops,
280                                    int numPolys,
281                                    float (*r_polyNors)[3],
282                                    const struct MFace *mfaces,
283                                    int numFaces,
284                                    const int *origIndexFace,
285                                    float (*r_faceNors)[3]);
286 void BKE_mesh_calc_normals_mapping_ex(struct MVert *mverts,
287                                       int numVerts,
288                                       const struct MLoop *mloop,
289                                       const struct MPoly *mpolys,
290                                       int numLoops,
291                                       int numPolys,
292                                       float (*r_polyNors)[3],
293                                       const struct MFace *mfaces,
294                                       int numFaces,
295                                       const int *origIndexFace,
296                                       float (*r_faceNors)[3],
297                                       const bool only_face_normals);
298 void BKE_mesh_calc_normals_poly(struct MVert *mverts,
299                                 float (*r_vertnors)[3],
300                                 int numVerts,
301                                 const struct MLoop *mloop,
302                                 const struct MPoly *mpolys,
303                                 int numLoops,
304                                 int numPolys,
305                                 float (*r_polyNors)[3],
306                                 const bool only_face_normals);
307 void BKE_mesh_calc_normals(struct Mesh *me);
308 void BKE_mesh_ensure_normals(struct Mesh *me);
309 void BKE_mesh_ensure_normals_for_display(struct Mesh *mesh);
310 void BKE_mesh_calc_normals_looptri(struct MVert *mverts,
311                                    int numVerts,
312                                    const struct MLoop *mloop,
313                                    const struct MLoopTri *looptri,
314                                    int looptri_num,
315                                    float (*r_tri_nors)[3]);
316 void BKE_mesh_loop_manifold_fan_around_vert_next(const struct MLoop *mloops,
317                                                  const struct MPoly *mpolys,
318                                                  const int *loop_to_poly,
319                                                  const int *e2lfan_curr,
320                                                  const uint mv_pivot_index,
321                                                  const struct MLoop **r_mlfan_curr,
322                                                  int *r_mlfan_curr_index,
323                                                  int *r_mlfan_vert_index,
324                                                  int *r_mpfan_curr_index);
325 
326 void BKE_edges_sharp_from_angle_set(const struct MVert *mverts,
327                                     const int numVerts,
328                                     struct MEdge *medges,
329                                     const int numEdges,
330                                     struct MLoop *mloops,
331                                     const int numLoops,
332                                     struct MPoly *mpolys,
333                                     const float (*polynors)[3],
334                                     const int numPolys,
335                                     const float split_angle);
336 
337 /**
338  * References a contiguous loop-fan with normal offset vars.
339  */
340 typedef struct MLoopNorSpace {
341   /** Automatically computed loop normal. */
342   float vec_lnor[3];
343   /** Reference vector, orthogonal to vec_lnor. */
344   float vec_ref[3];
345   /** Third vector, orthogonal to vec_lnor and vec_ref. */
346   float vec_ortho[3];
347   /** Reference angle, around vec_ortho, in ]0, pi] range (0.0 marks that space as invalid). */
348   float ref_alpha;
349   /** Reference angle, around vec_lnor, in ]0, 2pi] range (0.0 marks that space as invalid). */
350   float ref_beta;
351   /** All loops using this lnor space (i.e. smooth fan of loops),
352    * as (depending on owning MLoopNorSpaceArrary.data_type):
353    *     - Indices (uint_in_ptr), or
354    *     - BMLoop pointers. */
355   struct LinkNode *loops;
356   char flags;
357 
358   /** To be used for extended processing related to loop normal spaces (aka smooth fans). */
359   void *user_data;
360 } MLoopNorSpace;
361 /**
362  * MLoopNorSpace.flags
363  */
364 enum {
365   MLNOR_SPACE_IS_SINGLE = 1 << 0,
366 };
367 
368 /**
369  * Collection of #MLoopNorSpace basic storage & pre-allocation.
370  */
371 typedef struct MLoopNorSpaceArray {
372   MLoopNorSpace **lspacearr; /* MLoop aligned array */
373   struct LinkNode
374       *loops_pool; /* Allocated once, avoids to call BLI_linklist_prepend_arena() for each loop! */
375   char data_type;  /* Whether we store loop indices, or pointers to BMLoop. */
376   int num_spaces;  /* Number of clnors spaces defined in this array. */
377   struct MemArena *mem;
378 } MLoopNorSpaceArray;
379 /**
380  * MLoopNorSpaceArray.data_type
381  */
382 enum {
383   MLNOR_SPACEARR_LOOP_INDEX = 0,
384   MLNOR_SPACEARR_BMLOOP_PTR = 1,
385 };
386 
387 /* Low-level custom normals functions. */
388 void BKE_lnor_spacearr_init(MLoopNorSpaceArray *lnors_spacearr,
389                             const int numLoops,
390                             const char data_type);
391 void BKE_lnor_spacearr_clear(MLoopNorSpaceArray *lnors_spacearr);
392 void BKE_lnor_spacearr_free(MLoopNorSpaceArray *lnors_spacearr);
393 MLoopNorSpace *BKE_lnor_space_create(MLoopNorSpaceArray *lnors_spacearr);
394 void BKE_lnor_space_define(MLoopNorSpace *lnor_space,
395                            const float lnor[3],
396                            float vec_ref[3],
397                            float vec_other[3],
398                            struct BLI_Stack *edge_vectors);
399 void BKE_lnor_space_add_loop(MLoopNorSpaceArray *lnors_spacearr,
400                              MLoopNorSpace *lnor_space,
401                              const int ml_index,
402                              void *bm_loop,
403                              const bool is_single);
404 void BKE_lnor_space_custom_data_to_normal(MLoopNorSpace *lnor_space,
405                                           const short clnor_data[2],
406                                           float r_custom_lnor[3]);
407 void BKE_lnor_space_custom_normal_to_data(MLoopNorSpace *lnor_space,
408                                           const float custom_lnor[3],
409                                           short r_clnor_data[2]);
410 
411 /* Medium-level custom normals functions. */
412 void BKE_mesh_normals_loop_split(const struct MVert *mverts,
413                                  const int numVerts,
414                                  struct MEdge *medges,
415                                  const int numEdges,
416                                  struct MLoop *mloops,
417                                  float (*r_loopnors)[3],
418                                  const int numLoops,
419                                  struct MPoly *mpolys,
420                                  const float (*polynors)[3],
421                                  const int numPolys,
422                                  const bool use_split_normals,
423                                  const float split_angle,
424                                  MLoopNorSpaceArray *r_lnors_spacearr,
425                                  short (*clnors_data)[2],
426                                  int *r_loop_to_poly);
427 
428 void BKE_mesh_normals_loop_custom_set(const struct MVert *mverts,
429                                       const int numVerts,
430                                       struct MEdge *medges,
431                                       const int numEdges,
432                                       struct MLoop *mloops,
433                                       float (*r_custom_loopnors)[3],
434                                       const int numLoops,
435                                       struct MPoly *mpolys,
436                                       const float (*polynors)[3],
437                                       const int numPolys,
438                                       short (*r_clnors_data)[2]);
439 void BKE_mesh_normals_loop_custom_from_vertices_set(const struct MVert *mverts,
440                                                     float (*r_custom_vertnors)[3],
441                                                     const int numVerts,
442                                                     struct MEdge *medges,
443                                                     const int numEdges,
444                                                     struct MLoop *mloops,
445                                                     const int numLoops,
446                                                     struct MPoly *mpolys,
447                                                     const float (*polynors)[3],
448                                                     const int numPolys,
449                                                     short (*r_clnors_data)[2]);
450 
451 void BKE_mesh_normals_loop_to_vertex(const int numVerts,
452                                      const struct MLoop *mloops,
453                                      const int numLoops,
454                                      const float (*clnors)[3],
455                                      float (*r_vert_clnors)[3]);
456 
457 /* High-level custom normals functions. */
458 bool BKE_mesh_has_custom_loop_normals(struct Mesh *me);
459 
460 void BKE_mesh_calc_normals_split(struct Mesh *mesh);
461 void BKE_mesh_calc_normals_split_ex(struct Mesh *mesh,
462                                     struct MLoopNorSpaceArray *r_lnors_spacearr);
463 
464 void BKE_mesh_set_custom_normals(struct Mesh *mesh, float (*r_custom_loopnors)[3]);
465 void BKE_mesh_set_custom_normals_from_vertices(struct Mesh *mesh, float (*r_custom_vertnors)[3]);
466 
467 void BKE_mesh_calc_poly_normal(const struct MPoly *mpoly,
468                                const struct MLoop *loopstart,
469                                const struct MVert *mvarray,
470                                float r_no[3]);
471 void BKE_mesh_calc_poly_normal_coords(const struct MPoly *mpoly,
472                                       const struct MLoop *loopstart,
473                                       const float (*vertex_coords)[3],
474                                       float r_no[3]);
475 void BKE_mesh_calc_poly_center(const struct MPoly *mpoly,
476                                const struct MLoop *loopstart,
477                                const struct MVert *mvarray,
478                                float r_cent[3]);
479 float BKE_mesh_calc_poly_area(const struct MPoly *mpoly,
480                               const struct MLoop *loopstart,
481                               const struct MVert *mvarray);
482 float BKE_mesh_calc_area(const struct Mesh *me);
483 float BKE_mesh_calc_poly_uv_area(const struct MPoly *mpoly, const struct MLoopUV *uv_array);
484 void BKE_mesh_calc_poly_angles(const struct MPoly *mpoly,
485                                const struct MLoop *loopstart,
486                                const struct MVert *mvarray,
487                                float angles[]);
488 
489 void BKE_mesh_poly_edgehash_insert(struct EdgeHash *ehash,
490                                    const struct MPoly *mp,
491                                    const struct MLoop *mloop);
492 void BKE_mesh_poly_edgebitmap_insert(unsigned int *edge_bitmap,
493                                      const struct MPoly *mp,
494                                      const struct MLoop *mloop);
495 
496 bool BKE_mesh_center_median(const struct Mesh *me, float r_cent[3]);
497 bool BKE_mesh_center_median_from_polys(const struct Mesh *me, float r_cent[3]);
498 bool BKE_mesh_center_bounds(const struct Mesh *me, float r_cent[3]);
499 bool BKE_mesh_center_of_surface(const struct Mesh *me, float r_cent[3]);
500 bool BKE_mesh_center_of_volume(const struct Mesh *me, float r_cent[3]);
501 
502 void BKE_mesh_calc_volume(const struct MVert *mverts,
503                           const int mverts_num,
504                           const struct MLoopTri *mlooptri,
505                           const int looptri_num,
506                           const struct MLoop *mloop,
507                           float *r_volume,
508                           float r_center[3]);
509 
510 /* tessface */
511 void BKE_mesh_loops_to_mface_corners(struct CustomData *fdata,
512                                      struct CustomData *ldata,
513                                      struct CustomData *pdata,
514                                      unsigned int lindex[4],
515                                      int findex,
516                                      const int polyindex,
517                                      const int mf_len,
518                                      const int numUV,
519                                      const int numCol,
520                                      const bool hasPCol,
521                                      const bool hasOrigSpace,
522                                      const bool hasLNor);
523 void BKE_mesh_loops_to_tessdata(struct CustomData *fdata,
524                                 struct CustomData *ldata,
525                                 struct MFace *mface,
526                                 const int *polyindices,
527                                 unsigned int (*loopindices)[4],
528                                 const int num_faces);
529 void BKE_mesh_tangent_loops_to_tessdata(struct CustomData *fdata,
530                                         struct CustomData *ldata,
531                                         struct MFace *mface,
532                                         const int *polyindices,
533                                         unsigned int (*loopindices)[4],
534                                         const int num_faces,
535                                         const char *layer_name);
536 int BKE_mesh_tessface_calc_ex(struct CustomData *fdata,
537                               struct CustomData *ldata,
538                               struct CustomData *pdata,
539                               struct MVert *mvert,
540                               int totface,
541                               int totloop,
542                               int totpoly,
543                               const bool do_face_nor_copy);
544 void BKE_mesh_recalc_looptri(const struct MLoop *mloop,
545                              const struct MPoly *mpoly,
546                              const struct MVert *mvert,
547                              int totloop,
548                              int totpoly,
549                              struct MLoopTri *mlooptri);
550 void BKE_mesh_convert_mfaces_to_mpolys(struct Mesh *mesh);
551 void BKE_mesh_do_versions_convert_mfaces_to_mpolys(struct Mesh *mesh);
552 void BKE_mesh_convert_mfaces_to_mpolys_ex(struct ID *id,
553                                           struct CustomData *fdata,
554                                           struct CustomData *ldata,
555                                           struct CustomData *pdata,
556                                           int totedge_i,
557                                           int totface_i,
558                                           int totloop_i,
559                                           int totpoly_i,
560                                           struct MEdge *medge,
561                                           struct MFace *mface,
562                                           int *r_totloop,
563                                           int *r_totpoly,
564                                           struct MLoop **r_mloop,
565                                           struct MPoly **r_mpoly);
566 
567 void BKE_mesh_mdisp_flip(struct MDisps *md, const bool use_loop_mdisp_flip);
568 
569 void BKE_mesh_polygon_flip_ex(struct MPoly *mpoly,
570                               struct MLoop *mloop,
571                               struct CustomData *ldata,
572                               float (*lnors)[3],
573                               struct MDisps *mdisp,
574                               const bool use_loop_mdisp_flip);
575 void BKE_mesh_polygon_flip(struct MPoly *mpoly, struct MLoop *mloop, struct CustomData *ldata);
576 void BKE_mesh_polygons_flip(struct MPoly *mpoly,
577                             struct MLoop *mloop,
578                             struct CustomData *ldata,
579                             int totpoly);
580 
581 /* merge verts  */
582 /* Enum for merge_mode of CDDM_merge_verts.
583  * Refer to mesh.c for details. */
584 enum {
585   MESH_MERGE_VERTS_DUMP_IF_MAPPED,
586   MESH_MERGE_VERTS_DUMP_IF_EQUAL,
587 };
588 struct Mesh *BKE_mesh_merge_verts(struct Mesh *mesh,
589                                   const int *vtargetmap,
590                                   const int tot_vtargetmap,
591                                   const int merge_mode);
592 
593 /* flush flags */
594 void BKE_mesh_flush_hidden_from_verts_ex(const struct MVert *mvert,
595                                          const struct MLoop *mloop,
596                                          struct MEdge *medge,
597                                          const int totedge,
598                                          struct MPoly *mpoly,
599                                          const int totpoly);
600 void BKE_mesh_flush_hidden_from_verts(struct Mesh *me);
601 void BKE_mesh_flush_hidden_from_polys_ex(struct MVert *mvert,
602                                          const struct MLoop *mloop,
603                                          struct MEdge *medge,
604                                          const int totedge,
605                                          const struct MPoly *mpoly,
606                                          const int totpoly);
607 void BKE_mesh_flush_hidden_from_polys(struct Mesh *me);
608 void BKE_mesh_flush_select_from_polys_ex(struct MVert *mvert,
609                                          const int totvert,
610                                          const struct MLoop *mloop,
611                                          struct MEdge *medge,
612                                          const int totedge,
613                                          const struct MPoly *mpoly,
614                                          const int totpoly);
615 void BKE_mesh_flush_select_from_polys(struct Mesh *me);
616 void BKE_mesh_flush_select_from_verts_ex(const struct MVert *mvert,
617                                          const int totvert,
618                                          const struct MLoop *mloop,
619                                          struct MEdge *medge,
620                                          const int totedge,
621                                          struct MPoly *mpoly,
622                                          const int totpoly);
623 void BKE_mesh_flush_select_from_verts(struct Mesh *me);
624 
625 /* spatial evaluation */
626 void BKE_mesh_calc_relative_deform(const struct MPoly *mpoly,
627                                    const int totpoly,
628                                    const struct MLoop *mloop,
629                                    const int totvert,
630 
631                                    const float (*vert_cos_src)[3],
632                                    const float (*vert_cos_dst)[3],
633 
634                                    const float (*vert_cos_org)[3],
635                                    float (*vert_cos_new)[3]);
636 
637 /* *** mesh_validate.c *** */
638 
639 bool BKE_mesh_validate(struct Mesh *me, const bool do_verbose, const bool cddata_check_mask);
640 bool BKE_mesh_is_valid(struct Mesh *me);
641 bool BKE_mesh_validate_material_indices(struct Mesh *me);
642 
643 bool BKE_mesh_validate_arrays(struct Mesh *me,
644                               struct MVert *mverts,
645                               unsigned int totvert,
646                               struct MEdge *medges,
647                               unsigned int totedge,
648                               struct MFace *mfaces,
649                               unsigned int totface,
650                               struct MLoop *mloops,
651                               unsigned int totloop,
652                               struct MPoly *mpolys,
653                               unsigned int totpoly,
654                               struct MDeformVert *dverts, /* assume totvert length */
655                               const bool do_verbose,
656                               const bool do_fixes,
657                               bool *r_change);
658 
659 bool BKE_mesh_validate_all_customdata(struct CustomData *vdata,
660                                       const uint totvert,
661                                       struct CustomData *edata,
662                                       const uint totedge,
663                                       struct CustomData *ldata,
664                                       const uint totloop,
665                                       struct CustomData *pdata,
666                                       const uint totpoly,
667                                       const bool check_meshmask,
668                                       const bool do_verbose,
669                                       const bool do_fixes,
670                                       bool *r_change);
671 
672 void BKE_mesh_strip_loose_faces(struct Mesh *me);
673 void BKE_mesh_strip_loose_polysloops(struct Mesh *me);
674 void BKE_mesh_strip_loose_edges(struct Mesh *me);
675 
676 void BKE_mesh_calc_edges_legacy(struct Mesh *me, const bool use_old);
677 void BKE_mesh_calc_edges_loose(struct Mesh *mesh);
678 void BKE_mesh_calc_edges(struct Mesh *mesh, bool keep_existing_edges, const bool select_new_edges);
679 void BKE_mesh_calc_edges_tessface(struct Mesh *mesh);
680 
681 /* In DerivedMesh.c */
682 void BKE_mesh_wrapper_deferred_finalize(struct Mesh *me_eval,
683                                         const CustomData_MeshMasks *cd_mask_finalize);
684 
685 /* **** Depsgraph evaluation **** */
686 
687 void BKE_mesh_eval_geometry(struct Depsgraph *depsgraph, struct Mesh *mesh);
688 
689 /* Draw Cache */
690 void BKE_mesh_batch_cache_dirty_tag(struct Mesh *me, eMeshBatchDirtyMode mode);
691 void BKE_mesh_batch_cache_free(struct Mesh *me);
692 
693 extern void (*BKE_mesh_batch_cache_dirty_tag_cb)(struct Mesh *me, eMeshBatchDirtyMode mode);
694 extern void (*BKE_mesh_batch_cache_free_cb)(struct Mesh *me);
695 
696 /* Inlines */
697 
698 /* Instead of -1 that function uses ORIGINDEX_NONE as defined in BKE_customdata.h,
699  * but I don't want to force every user of BKE_mesh.h to also include that file.
700  * ~~ Sybren */
BKE_mesh_origindex_mface_mpoly(const int * index_mf_to_mpoly,const int * index_mp_to_orig,const int i)701 BLI_INLINE int BKE_mesh_origindex_mface_mpoly(const int *index_mf_to_mpoly,
702                                               const int *index_mp_to_orig,
703                                               const int i)
704 {
705   const int j = index_mf_to_mpoly[i];
706   return (j != -1) ? (index_mp_to_orig ? index_mp_to_orig[j] : j) : -1;
707 }
708 
709 #ifdef __cplusplus
710 }
711 #endif
712