BKE_ccg.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * ***** BEGIN GPL LICENSE BLOCK *****
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License
  6. * as published by the Free Software Foundation; either version 2
  7. * of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software Foundation,
  16. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  17. *
  18. * The Original Code is Copyright (C) 2012 by Nicholas Bishop.
  19. * All rights reserved.
  20. *
  21. * The Original Code is: all of this file.
  22. *
  23. * Contributor(s): none yet.
  24. *
  25. * ***** END GPL LICENSE BLOCK *****
  26. */
  27. #ifndef __BKE_CCG_H__
  28. #define __BKE_CCG_H__
  29. /** \file BKE_ccg.h
  30. * \ingroup bke
  31. */
  32. /* defines BLI_INLINE */
  33. #include "BLI_utildefines.h"
  34. /* declares fprintf() and abort(), needed for BLI_assert */
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. struct CCGSubSurf;
  38. /* Each CCGElem is CCGSubSurf's representation of a subdivided
  39. * vertex. All CCGElems in a particular CCGSubSurf have the same
  40. * layout, but the layout can vary from one CCGSubSurf to another. For
  41. * this reason, CCGElem is presented as an opaque pointer, and
  42. * elements should always be accompanied by a CCGKey, which provides
  43. * the necessary offsets to access components of a CCGElem.
  44. */
  45. typedef struct CCGElem CCGElem;
  46. typedef struct CCGKey {
  47. int level;
  48. /* number of bytes in each element (one float per layer, plus
  49. * three floats for normals if enabled) */
  50. int elem_size;
  51. /* number of elements along each side of grid */
  52. int grid_size;
  53. /* number of elements in the grid (grid size squared) */
  54. int grid_area;
  55. /* number of bytes in each grid (grid_area * elem_size) */
  56. int grid_bytes;
  57. /* currently always the last three floats, unless normals are
  58. * disabled */
  59. int normal_offset;
  60. /* offset in bytes of mask value; only valid if 'has_mask' is
  61. * true */
  62. int mask_offset;
  63. int num_layers;
  64. int has_normals;
  65. int has_mask;
  66. } CCGKey;
  67. /* initialize 'key' at the specified level */
  68. void CCG_key(CCGKey *key, const struct CCGSubSurf *ss, int level);
  69. void CCG_key_top_level(CCGKey *key, const struct CCGSubSurf *ss);
  70. /* get a pointer to the coordinate, normal, or mask components */
  71. BLI_INLINE float *CCG_elem_co(const CCGKey *key, CCGElem *elem);
  72. BLI_INLINE float *CCG_elem_no(const CCGKey *key, CCGElem *elem);
  73. BLI_INLINE float *CCG_elem_mask(const CCGKey *key, CCGElem *elem);
  74. /* get the element at 'offset' in an array */
  75. BLI_INLINE CCGElem *CCG_elem_offset(const CCGKey *key, CCGElem *elem, int offset);
  76. /* get the element at coordinate (x,y) in a face-grid array */
  77. BLI_INLINE CCGElem *CCG_grid_elem(const CCGKey *key, CCGElem *elem, int x, int y);
  78. /* combinations of above functions */
  79. BLI_INLINE float *CCG_grid_elem_co(const CCGKey *key, CCGElem *elem, int x, int y);
  80. BLI_INLINE float *CCG_grid_elem_no(const CCGKey *key, CCGElem *elem, int x, int y);
  81. BLI_INLINE float *CCG_grid_elem_mask(const CCGKey *key, CCGElem *elem, int x, int y);
  82. BLI_INLINE float *CCG_elem_offset_co(const CCGKey *key, CCGElem *elem, int offset);
  83. BLI_INLINE float *CCG_elem_offset_no(const CCGKey *key, CCGElem *elem, int offset);
  84. BLI_INLINE float *CCG_elem_offset_mask(const CCGKey *key, CCGElem *elem, int offset);
  85. /* for iteration, get a pointer to the next element in an array */
  86. BLI_INLINE CCGElem *CCG_elem_next(const CCGKey *key, CCGElem *elem);
  87. /* inline definitions follow */
  88. BLI_INLINE float *CCG_elem_co(const CCGKey *UNUSED(key), CCGElem *elem)
  89. {
  90. return (float *)elem;
  91. }
  92. BLI_INLINE float *CCG_elem_no(const CCGKey *key, CCGElem *elem)
  93. {
  94. BLI_assert(key->has_normals);
  95. return (float *)((char *)elem + key->normal_offset);
  96. }
  97. BLI_INLINE float *CCG_elem_mask(const CCGKey *key, CCGElem *elem)
  98. {
  99. BLI_assert(key->has_mask);
  100. return (float *)((char *)elem + (key->mask_offset));
  101. }
  102. BLI_INLINE CCGElem *CCG_elem_offset(const CCGKey *key, CCGElem *elem, int offset)
  103. {
  104. return (CCGElem *)(((char *)elem) + key->elem_size * offset);
  105. }
  106. BLI_INLINE CCGElem *CCG_grid_elem(const CCGKey *key, CCGElem *elem, int x, int y)
  107. {
  108. // BLI_assert(x < key->grid_size && y < key->grid_size);
  109. return CCG_elem_offset(key, elem, (y * key->grid_size + x));
  110. }
  111. BLI_INLINE float *CCG_grid_elem_co(const CCGKey *key, CCGElem *elem, int x, int y)
  112. {
  113. return CCG_elem_co(key, CCG_grid_elem(key, elem, x, y));
  114. }
  115. BLI_INLINE float *CCG_grid_elem_no(const CCGKey *key, CCGElem *elem, int x, int y)
  116. {
  117. return CCG_elem_no(key, CCG_grid_elem(key, elem, x, y));
  118. }
  119. BLI_INLINE float *CCG_grid_elem_mask(const CCGKey *key, CCGElem *elem, int x, int y)
  120. {
  121. return CCG_elem_mask(key, CCG_grid_elem(key, elem, x, y));
  122. }
  123. BLI_INLINE float *CCG_elem_offset_co(const CCGKey *key, CCGElem *elem, int offset)
  124. {
  125. return CCG_elem_co(key, CCG_elem_offset(key, elem, offset));
  126. }
  127. BLI_INLINE float *CCG_elem_offset_no(const CCGKey *key, CCGElem *elem, int offset)
  128. {
  129. return CCG_elem_no(key, CCG_elem_offset(key, elem, offset));
  130. }
  131. BLI_INLINE float *CCG_elem_offset_mask(const CCGKey *key, CCGElem *elem, int offset)
  132. {
  133. return CCG_elem_mask(key, CCG_elem_offset(key, elem, offset));
  134. }
  135. BLI_INLINE CCGElem *CCG_elem_next(const CCGKey *key, CCGElem *elem)
  136. {
  137. return CCG_elem_offset(key, elem, 1);
  138. }
  139. #endif