Blender V5.0
BKE_ccg.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2012 by Nicholas Bishop. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
10
11#include "BLI_assert.h"
13
14struct CCGSubSurf;
15
16/* Each CCGElem is CCGSubSurf's representation of a subdivided
17 * vertex. All CCGElems in a particular CCGSubSurf have the same
18 * layout, but the layout can vary from one CCGSubSurf to another. For
19 * this reason, CCGElem is presented as an opaque pointer, and
20 * elements should always be accompanied by a CCGKey, which provides
21 * the necessary offsets to access components of a CCGElem.
22 */
23struct CCGElem;
24
25struct CCGKey {
26 int level;
27
28 /* number of bytes in each element (one float per layer, plus
29 * three floats for normals if enabled) */
31
32 /* number of elements along each side of grid */
34 /* number of elements in the grid (grid size squared) */
36 /* number of bytes in each grid (grid_area * elem_size) */
38
39 /* currently always the last three floats, unless normals are
40 * disabled */
42
43 /* offset in bytes of mask value; only valid if 'has_mask' is
44 * true */
46
49};
50
51inline blender::float3 &CCG_elem_co(const CCGKey & /*key*/, CCGElem *elem)
52{
53 return *reinterpret_cast<blender::float3 *>(elem);
54}
55
56inline blender::float3 &CCG_elem_no(const CCGKey &key, CCGElem *elem)
57{
59 return *reinterpret_cast<blender::float3 *>(reinterpret_cast<char *>(elem) + key.normal_offset);
60}
61
62inline float &CCG_elem_mask(const CCGKey &key, CCGElem *elem)
63{
65 return *reinterpret_cast<float *>(reinterpret_cast<char *>(elem) + (key.mask_offset));
66}
67
68inline CCGElem *CCG_elem_offset(const CCGKey &key, CCGElem *elem, int offset)
69{
70 return reinterpret_cast<CCGElem *>((reinterpret_cast<char *>(elem)) + key.elem_size * offset);
71}
72
73inline int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
74{
75 return y * grid_size + x;
76}
77
78inline CCGElem *CCG_grid_elem(const CCGKey &key, CCGElem *elem, int x, int y)
79{
80 // BLI_assert(x < key.grid_size && y < key.grid_size);
81 return CCG_elem_offset(key, elem, CCG_grid_xy_to_index(key.grid_size, x, y));
82}
83
84inline blender::float3 &CCG_grid_elem_co(const CCGKey &key, CCGElem *elem, int x, int y)
85{
86 return CCG_elem_co(key, CCG_grid_elem(key, elem, x, y));
87}
88
89inline blender::float3 &CCG_grid_elem_no(const CCGKey &key, CCGElem *elem, int x, int y)
90{
91 return CCG_elem_no(key, CCG_grid_elem(key, elem, x, y));
92}
93
94inline float &CCG_grid_elem_mask(const CCGKey &key, CCGElem *elem, int x, int y)
95{
96 return CCG_elem_mask(key, CCG_grid_elem(key, elem, x, y));
97}
98
99inline blender::float3 &CCG_elem_offset_co(const CCGKey &key, CCGElem *elem, int offset)
100{
101 return CCG_elem_co(key, CCG_elem_offset(key, elem, offset));
102}
103
104inline int CCG_grid_size(const int level)
105{
106 BLI_assert(level > 0);
107 return (1 << (level - 1)) + 1;
108}
109
110inline int CCG_grid_factor(int low_level, int high_level)
111{
112 BLI_assert(low_level > 0 && high_level > 0);
113 BLI_assert(low_level <= high_level);
114 return 1 << (high_level - low_level);
115}
int CCG_grid_factor(int low_level, int high_level)
Definition BKE_ccg.hh:110
blender::float3 & CCG_grid_elem_no(const CCGKey &key, CCGElem *elem, int x, int y)
Definition BKE_ccg.hh:89
blender::float3 & CCG_grid_elem_co(const CCGKey &key, CCGElem *elem, int x, int y)
Definition BKE_ccg.hh:84
int CCG_grid_xy_to_index(const int grid_size, const int x, const int y)
Definition BKE_ccg.hh:73
CCGElem * CCG_grid_elem(const CCGKey &key, CCGElem *elem, int x, int y)
Definition BKE_ccg.hh:78
blender::float3 & CCG_elem_offset_co(const CCGKey &key, CCGElem *elem, int offset)
Definition BKE_ccg.hh:99
float & CCG_elem_mask(const CCGKey &key, CCGElem *elem)
Definition BKE_ccg.hh:62
blender::float3 & CCG_elem_no(const CCGKey &key, CCGElem *elem)
Definition BKE_ccg.hh:56
blender::float3 & CCG_elem_co(const CCGKey &, CCGElem *elem)
Definition BKE_ccg.hh:51
CCGElem * CCG_elem_offset(const CCGKey &key, CCGElem *elem, int offset)
Definition BKE_ccg.hh:68
float & CCG_grid_elem_mask(const CCGKey &key, CCGElem *elem, int x, int y)
Definition BKE_ccg.hh:94
int CCG_grid_size(const int level)
Definition BKE_ccg.hh:104
#define BLI_assert(a)
Definition BLI_assert.h:46
VecBase< float, 3 > float3
int has_mask
Definition BKE_ccg.hh:48
int mask_offset
Definition BKE_ccg.hh:45
int grid_size
Definition BKE_ccg.hh:33
int grid_bytes
Definition BKE_ccg.hh:37
int grid_area
Definition BKE_ccg.hh:35
int level
Definition BKE_ccg.hh:26
int normal_offset
Definition BKE_ccg.hh:41
int elem_size
Definition BKE_ccg.hh:30
int has_normals
Definition BKE_ccg.hh:47