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 /** \file
21  * \ingroup DNA
22  */
23 
24 #pragma once
25 
26 #include "DNA_customdata_types.h"
27 #include "DNA_listBase.h"
28 
29 /* -------------------------------------------------------------------- */
30 /** \name Geometry Elements
31  * \{ */
32 
33 /**
34  * Mesh Vertices.
35  *
36  * Typically accessed from #Mesh.mvert
37  */
38 typedef struct MVert {
39   float co[3];
40   /**
41    * Cache the normal, can always be recalculated from surrounding faces.
42    * See #CD_CUSTOMLOOPNORMAL for custom normals.
43    */
44   short no[3];
45   char flag, bweight;
46 } MVert;
47 
48 /** #MVert.flag */
49 enum {
50   /*  SELECT = (1 << 0), */
51   ME_VERT_TMP_TAG = (1 << 2),
52   ME_HIDE = (1 << 4),
53   ME_VERT_FACEDOT = (1 << 5),
54   /*  ME_VERT_MERGED = (1 << 6), */
55   ME_VERT_PBVH_UPDATE = (1 << 7),
56 };
57 
58 /**
59  * Mesh Edges.
60  *
61  * Typically accessed from #Mesh.medge
62  */
63 typedef struct MEdge {
64   /** Un-ordered vertex indices (cannot match). */
65   unsigned int v1, v2;
66   char crease, bweight;
67   short flag;
68 } MEdge;
69 
70 /** #MEdge.flag */
71 enum {
72   /*  SELECT = (1 << 0), */
73   ME_EDGEDRAW = (1 << 1),
74   ME_SEAM = (1 << 2),
75   /*  ME_HIDE = (1 << 4), */
76   ME_EDGERENDER = (1 << 5),
77   ME_LOOSEEDGE = (1 << 7),
78   ME_EDGE_TMP_TAG = (1 << 8),
79   ME_SHARP = (1 << 9), /* only reason this flag remains a 'short' */
80 };
81 
82 /**
83  * Mesh Faces
84  * This only stores the polygon size & flags, the vertex & edge indices are stored in the #MLoop.
85  *
86  * Typically accessed from #Mesh.mpoly.
87  */
88 typedef struct MPoly {
89   /** Offset into loop array and number of loops in the face. */
90   int loopstart;
91   /** Keep signed since we need to subtract when getting the previous loop. */
92   int totloop;
93   short mat_nr;
94   char flag, _pad;
95 } MPoly;
96 
97 /** #MPoly.flag */
98 enum {
99   ME_SMOOTH = (1 << 0),
100   ME_FACE_SEL = (1 << 1),
101   /* ME_HIDE = (1 << 4), */
102 };
103 
104 /**
105  * Mesh Loops.
106  * Each loop represents the corner of a polygon (#MPoly).
107  *
108  * Typically accessed from #Mesh.mloop.
109  */
110 typedef struct MLoop {
111   /** Vertex index. */
112   unsigned int v;
113   /**
114    * Edge index.
115    *
116    * \note The e here is because we want to move away from relying on edge hashes.
117    */
118   unsigned int e;
119 } MLoop;
120 
121 /** \} */
122 
123 /* -------------------------------------------------------------------- */
124 /** \name Ordered Selection Storage
125  * \{ */
126 
127 /**
128  * Optionally store the order of selected elements.
129  * This wont always be set since only some selection operations have an order.
130  *
131  * Typically accessed from #Mesh.mselect
132  */
133 typedef struct MSelect {
134   /** Index in the vertex, edge or polygon array. */
135   int index;
136   /** #ME_VSEL, #ME_ESEL, #ME_FSEL. */
137   int type;
138 } MSelect;
139 
140 /** #MSelect.type */
141 enum {
142   ME_VSEL = 0,
143   ME_ESEL = 1,
144   ME_FSEL = 2,
145 };
146 
147 /** \} */
148 
149 /* -------------------------------------------------------------------- */
150 /** \name Loop Tesselation Runtime Data
151  * \{ */
152 
153 /**
154  * #MLoopTri's are lightweight triangulation data,
155  * for functionality that doesn't support ngons (#MPoly).
156  * This is cache data created from (#MPoly, #MLoop & #MVert arrays).
157  * There is no attempt to maintain this data's validity over time,
158  * any changes to the underlying mesh invalidate the #MLoopTri array,
159  * which will need to be re-calculated.
160  *
161  * Users normally access this via #BKE_mesh_runtime_looptri_ensure.
162  * In rare cases its calculated directly, with #BKE_mesh_recalc_looptri.
163  *
164  * Typical usage includes:
165  * - OpenGL drawing.
166  * - #BVHTree creation.
167  * - Physics/collision detection.
168  *
169  * Storing loop indices (instead of vertex indices) allows us to
170  * directly access UV's, vertex-colors as well as vertices.
171  * The index of the source polygon is stored as well,
172  * giving access to materials and polygon normals.
173  *
174  * \note This data is runtime only, never written to disk.
175  *
176  * Usage examples:
177  * \code{.c}
178  * // access original material.
179  * short mat_nr = mpoly[lt->poly].mat_nr;
180  *
181  * // access vertex locations.
182  * float *vtri_co[3] = {
183  *     mvert[mloop[lt->tri[0]].v].co,
184  *     mvert[mloop[lt->tri[1]].v].co,
185  *     mvert[mloop[lt->tri[2]].v].co,
186  * };
187  *
188  * // access UV coordinates (works for all loop data, vertex colors... etc).
189  * float *uvtri_co[3] = {
190  *     mloopuv[lt->tri[0]].uv,
191  *     mloopuv[lt->tri[1]].uv,
192  *     mloopuv[lt->tri[2]].uv,
193  * };
194  * \endcode
195  *
196  * #MLoopTri's are allocated in an array, where each polygon's #MLoopTri's are stored contiguously,
197  * the number of triangles for each polygon is guaranteed to be (#MPoly.totloop - 2),
198  * even for degenerate geometry. See #ME_POLY_TRI_TOT macro.
199  *
200  * It's also possible to perform a reverse lookup (find all #MLoopTri's for any given #MPoly).
201  *
202  * \code{.c}
203  * // loop over all looptri's for a given polygon: i
204  * MPoly *mp = &mpoly[i];
205  * MLoopTri *lt = &looptri[poly_to_tri_count(i, mp->loopstart)];
206  * int j, lt_tot = ME_POLY_TRI_TOT(mp);
207  *
208  * for (j = 0; j < lt_tot; j++, lt++) {
209  *     unsigned int vtri[3] = {
210  *         mloop[lt->tri[0]].v,
211  *         mloop[lt->tri[1]].v,
212  *         mloop[lt->tri[2]].v,
213  *     };
214  *     printf("tri %u %u %u\n", vtri[0], vtri[1], vtri[2]);
215  * };
216  * \endcode
217  *
218  * It may also be useful to check whether or not two vertices of a triangle
219  * form an edge in the underlying mesh.
220  *
221  * This can be done by checking the edge of the referenced loop (#MLoop.e),
222  * the winding of the #MLoopTri and the #MLoop's will always match,
223  * however the order of vertices in the edge is undefined.
224  *
225  * \code{.c}
226  * // print real edges from an MLoopTri: lt
227  * int j, j_next;
228  * for (j = 2, j_next = 0; j_next < 3; j = j_next++) {
229  *     MEdge *ed = &medge[mloop[lt->tri[j]].e];
230  *     unsigned int tri_edge[2]  = {mloop[lt->tri[j]].v, mloop[lt->tri[j_next]].v};
231  *
232  *     if (((ed->v1 == tri_edge[0]) && (ed->v2 == tri_edge[1])) ||
233  *         ((ed->v1 == tri_edge[1]) && (ed->v2 == tri_edge[0])))
234  *     {
235  *         printf("real edge found %u %u\n", tri_edge[0], tri_edge[1]);
236  *     }
237  * }
238  * \endcode
239  *
240  * See #BKE_mesh_looptri_get_real_edges for a utility that does this.
241  *
242  * \note A #MLoopTri may be in the middle of an ngon and not reference **any** edges.
243  */
244 typedef struct MLoopTri {
245   unsigned int tri[3];
246   unsigned int poly;
247 } MLoopTri;
248 #
249 #
250 typedef struct MVertTri {
251   unsigned int tri[3];
252 } MVertTri;
253 
254 /** \} */
255 
256 /* -------------------------------------------------------------------- */
257 /** \name Custom Data (Generic)
258  * \{ */
259 
260 /** Custom Data Properties */
261 typedef struct MFloatProperty {
262   float f;
263 } MFloatProperty;
264 typedef struct MIntProperty {
265   int i;
266 } MIntProperty;
267 typedef struct MStringProperty {
268   char s[255], s_len;
269 } MStringProperty;
270 
271 /** \} */
272 
273 /* -------------------------------------------------------------------- */
274 /** \name Custom Data (Vertex)
275  * \{ */
276 
277 /**
278  * Vertex group index and weight for #MDeformVert.dw
279  */
280 typedef struct MDeformWeight {
281   /** The index for the vertex group, must *always* be unique when in an array. */
282   unsigned int def_nr;
283   /** Weight between 0.0 and 1.0. */
284   float weight;
285 } MDeformWeight;
286 
287 typedef struct MDeformVert {
288   struct MDeformWeight *dw;
289   int totweight;
290   /** Flag is only in use as a run-time tag at the moment. */
291   int flag;
292 } MDeformVert;
293 
294 typedef struct MVertSkin {
295   /**
296    * Radii of the skin, define how big the generated frames are.
297    * Currently only the first two elements are used.
298    */
299   float radius[3];
300 
301   /** #eMVertSkinFlag */
302   int flag;
303 } MVertSkin;
304 
305 typedef enum eMVertSkinFlag {
306   /** Marks a vertex as the edge-graph root, used for calculating rotations for all connected
307    * edges (recursively). Also used to choose a root when generating an armature.
308    */
309   MVERT_SKIN_ROOT = 1,
310 
311   /** Marks a branch vertex (vertex with more than two connected edges), so that its neighbors
312    * are directly hulled together, rather than the default of generating intermediate frames.
313    */
314   MVERT_SKIN_LOOSE = 2,
315 } eMVertSkinFlag;
316 
317 /** \} */
318 
319 /* -------------------------------------------------------------------- */
320 /** \name Custom Data (Loop)
321  * \{ */
322 
323 /**
324  * UV coordinate for a polygon face & flag for selection & other options.
325  */
326 typedef struct MLoopUV {
327   float uv[2];
328   int flag;
329 } MLoopUV;
330 
331 /** #MLoopUV.flag */
332 enum {
333   /* MLOOPUV_DEPRECATED = (1 << 0), MLOOPUV_EDGESEL removed */
334   MLOOPUV_VERTSEL = (1 << 1),
335   MLOOPUV_PINNED = (1 << 2),
336 };
337 
338 /**
339  * \note While alpha is currently is not in the view-port,
340  * this may eventually be added back, keep this value set to 255.
341  */
342 typedef struct MLoopCol {
343   unsigned char r, g, b, a;
344 } MLoopCol;
345 
346 typedef struct MPropCol {
347   float color[4];
348 } MPropCol;
349 
350 /** Multi-Resolution loop data. */
351 typedef struct MDisps {
352   /* Strange bug in SDNA: if disps pointer comes first, it fails to see totdisp */
353   int totdisp;
354   int level;
355   float (*disps)[3];
356 
357   /**
358    * Used for hiding parts of a multires mesh.
359    * Essentially the multires equivalent of #MVert.flag's ME_HIDE bit.
360    *
361    * \note This is a bitmap, keep in sync with type used in BLI_bitmap.h
362    */
363   unsigned int *hidden;
364 } MDisps;
365 
366 /** Multi-Resolution grid loop data. */
367 typedef struct GridPaintMask {
368   /**
369    * The data array contains `grid_size * grid_size` elements.
370    * Where `grid_size = (1 << (level - 1)) + 1`.
371    */
372   float *data;
373 
374   /** The maximum multires level associated with this grid. */
375   unsigned int level;
376 
377   char _pad[4];
378 } GridPaintMask;
379 
380 /** \} */
381 
382 /* -------------------------------------------------------------------- */
383 /** \name Custom Data (Original Space for Poly, Face)
384  * \{ */
385 
386 /**
387  * Original space within a face (similar to UV coordinates),
388  * however they are used to determine the original position in a face.
389  *
390  * Unlike UV's these are not user editable and always start out using a fixed 0-1 range.
391  * Currently only used for particle placement.
392  */
393 #
394 #
395 typedef struct OrigSpaceFace {
396   float uv[4][2];
397 } OrigSpaceFace;
398 
399 #
400 #
401 typedef struct OrigSpaceLoop {
402   float uv[2];
403 } OrigSpaceLoop;
404 
405 /** \} */
406 
407 /* -------------------------------------------------------------------- */
408 /** \name Custom Data (FreeStyle for Edge, Face)
409  * \{ */
410 
411 typedef struct FreestyleEdge {
412   char flag;
413   char _pad[3];
414 } FreestyleEdge;
415 
416 /** #FreestyleEdge.flag */
417 enum {
418   FREESTYLE_EDGE_MARK = 1,
419 };
420 
421 typedef struct FreestyleFace {
422   char flag;
423   char _pad[3];
424 } FreestyleFace;
425 
426 /** #FreestyleFace.flag */
427 enum {
428   FREESTYLE_FACE_MARK = 1,
429 };
430 
431 /** \} */
432 
433 /* -------------------------------------------------------------------- */
434 /** \name Utility Macros
435  * \{ */
436 
437 #define ME_POLY_LOOP_PREV(mloop, mp, i) \
438   (&(mloop)[(mp)->loopstart + (((i) + (mp)->totloop - 1) % (mp)->totloop)])
439 #define ME_POLY_LOOP_NEXT(mloop, mp, i) (&(mloop)[(mp)->loopstart + (((i) + 1) % (mp)->totloop)])
440 
441 /** Number of tri's that make up this polygon once tessellated. */
442 #define ME_POLY_TRI_TOT(mp) ((mp)->totloop - 2)
443 
444 /**
445  * Check out-of-bounds material, note that this is nearly always prevented,
446  * yet its still possible in rare cases.
447  * So usage such as array lookup needs to check.
448  */
449 #define ME_MAT_NR_TEST(mat_nr, totmat) \
450   (CHECK_TYPE_ANY(mat_nr, short, const short), \
451    CHECK_TYPE_ANY(totmat, short, const short), \
452    (LIKELY(mat_nr < totmat) ? mat_nr : 0))
453 
454 /** \} */
455 
456 /* -------------------------------------------------------------------- */
457 /** \name Deprecated Structs
458  * \{ */
459 
460 /**
461  * Used in Blender pre 2.63, See #MLoop, #MPoly for face data stored in the blend file.
462  * Use for reading old files and in a handful of cases which should be removed eventually.
463  */
464 typedef struct MFace {
465   unsigned int v1, v2, v3, v4;
466   short mat_nr;
467   /** We keep edcode, for conversion to edges draw flags in old files. */
468   char edcode, flag;
469 } MFace;
470 
471 /** #MFace.edcode */
472 enum {
473   ME_V1V2 = (1 << 0),
474   ME_V2V3 = (1 << 1),
475   ME_V3V1 = (1 << 2),
476   ME_V3V4 = ME_V3V1,
477   ME_V4V1 = (1 << 3),
478 };
479 
480 /** Tessellation uv face data. */
481 typedef struct MTFace {
482   float uv[4][2];
483 } MTFace;
484 
485 /**
486  * Tessellation vertex color data.
487  *
488  * \note The red and blue are swapped for historical reasons.
489  */
490 typedef struct MCol {
491   unsigned char a, r, g, b;
492 } MCol;
493 
494 #define MESH_MLOOPCOL_FROM_MCOL(_mloopcol, _mcol) \
495   { \
496     MLoopCol *mloopcol__tmp = _mloopcol; \
497     const MCol *mcol__tmp = _mcol; \
498     mloopcol__tmp->r = mcol__tmp->b; \
499     mloopcol__tmp->g = mcol__tmp->g; \
500     mloopcol__tmp->b = mcol__tmp->r; \
501     mloopcol__tmp->a = mcol__tmp->a; \
502   } \
503   (void)0
504 
505 #define MESH_MLOOPCOL_TO_MCOL(_mloopcol, _mcol) \
506   { \
507     const MLoopCol *mloopcol__tmp = _mloopcol; \
508     MCol *mcol__tmp = _mcol; \
509     mcol__tmp->b = mloopcol__tmp->r; \
510     mcol__tmp->g = mloopcol__tmp->g; \
511     mcol__tmp->r = mloopcol__tmp->b; \
512     mcol__tmp->a = mloopcol__tmp->a; \
513   } \
514   (void)0
515 
516 /** Old game engine recast navigation data, while unused 2.7x files may contain this. */
517 typedef struct MRecast {
518   int i;
519 } MRecast;
520 
521 /** Multires structs kept for compatibility with old files. */
522 typedef struct MultiresCol {
523   float a, r, g, b;
524 } MultiresCol;
525 
526 typedef struct MultiresColFace {
527   /* vertex colors */
528   MultiresCol col[4];
529 } MultiresColFace;
530 
531 typedef struct MultiresFace {
532   unsigned int v[4];
533   unsigned int mid;
534   char flag, mat_nr, _pad[2];
535 } MultiresFace;
536 
537 typedef struct MultiresEdge {
538   unsigned int v[2];
539   unsigned int mid;
540 } MultiresEdge;
541 
542 typedef struct MultiresLevel {
543   struct MultiresLevel *next, *prev;
544 
545   MultiresFace *faces;
546   MultiresColFace *colfaces;
547   MultiresEdge *edges;
548 
549   unsigned int totvert, totface, totedge;
550   char _pad[4];
551 
552   /* Kept for compatibility with even older files */
553   MVert *verts;
554 } MultiresLevel;
555 
556 typedef struct Multires {
557   ListBase levels;
558   MVert *verts;
559 
560   unsigned char level_count, current, newlvl, edgelvl, pinlvl, renderlvl;
561   unsigned char use_col, flag;
562 
563   /* Special level 1 data that cannot be modified from other levels */
564   CustomData vdata;
565   CustomData fdata;
566   short *edge_flags;
567   char *edge_creases;
568 } Multires;
569 /* End multi-res structs. */
570 
571 /** \} */
572